Skip to content

Commit

Permalink
NEW Allow Populate to build on live environments via config (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispenny authored Oct 21, 2021
1 parent 7b0764e commit 0c40e4e
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 11 deletions.
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ classes. For instance, when a building a web application the pages and default
objects can be defined in YAML and shared around developers. This extends the
`requireDefaultRecords` concept in SilverStripe's DataModel.

* [Requirements](#requirements)
* [Installation Instructions](#installation-instructions)
* [Setup](#setup)
* [Configuration options](#configuration-options)
* [YAML Format](#yaml-format)
* [Updating Records](#updating-records)
* [`PopulateMergeWhen`](#populatemergewhen)
* [`PopulateMergeMatch`](#populatemergematch)
* [`PopulateMergeAny`](#populatemergeany)
* [Default Assets](#default-assets)
* [Extensions](#extensions)
* [PopulateMySQLExport](#populatemysqlexport)
* [Publish configuration](#publish-configuration)
* [Allow Populate to run on "live" environments](#allow-populate-to-run-on-live-environments)

## Requirements

* PHP 7.1
Expand Down Expand Up @@ -217,16 +232,6 @@ Mysite\PageTypes\Product:
The module also provides extensions that can be opted into depending on your
project needs

## Publish configuration

By default the module uses `publishSingle()` to publish records. If, for whatever reason, you would prefer to that the
module uses `publishRecursive()`, you can enable this by settings the following configuration:

```yaml
DNADesign\Populate\Populate:
enable_publish_recursive: true
```

### PopulateMySQLExport

This extension outputs the result of the Populate::requireDefaultRecords() as a
Expand All @@ -247,6 +252,27 @@ DNADesign\Populate\Populate:
- DNADesign\Populate\PopulateMySQLExportExtension
```

## Publish configuration

By default the module uses `publishSingle()` to publish records. If, for whatever reason, you would prefer to that the
module uses `publishRecursive()`, you can enable this by settings the following configuration:

```yaml
DNADesign\Populate\Populate:
enable_publish_recursive: true
```

## Allow Populate to run on "live" environments

**DANGER ZONE:** Please understand that you are about to provide admins with the ability to run Populate on your
production environment. Before setting this configuration you should understand and accept the risks related to the
loss of production data.

```yaml
DNADesign\Populate\Populate:
allow_build_on_live: true
```

## Credits

silverstripe-populate was originally created by [wilr](https://github.com/wilr) and [DNA Design](https://www.dna.co.nz/).
13 changes: 12 additions & 1 deletion code/Populate.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function requireRecords($force = false): bool

self::$ran = true;

if (!(Director::isDev() || Director::isTest())) {
if (!self::canBuildOnEnvironment()) {
throw new Exception('requireRecords can only be run in development or test environments');
}

Expand Down Expand Up @@ -203,4 +203,15 @@ private static function truncateTable(string $table): void

self::$clearedTables[$table] = true;
}

private static function canBuildOnEnvironment(): bool
{
// Populate (by default) is allowed to run on dev and test environments
if (Director::isDev() || Director::isTest()) {
return true;
}

// Check if developer/s have specified that Populate can run on live
return (bool) self::config()->get('allow_build_on_live');
}
}
97 changes: 97 additions & 0 deletions tests/php/PopulateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace DNADesign\Populate\Tests;

use DNADesign\Populate\Populate;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use SilverStripe\Dev\SapphireTest;
use ReflectionClass;

class PopulateTest extends SapphireTest
{
private $environment = null;

public function testAllowOnDev(): void
{
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
$kernel->setEnvironment('dev');

// Use Reflection so that we can access private method
$reflection = new ReflectionClass(Populate::class);
$method = $reflection->getMethod('canBuildOnEnvironment');
$method->setAccessible(true);

// Invoke with null object, as this method is static
$this->assertTrue($method->invoke(null));
}

public function testAllowOnTest(): void
{
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
$kernel->setEnvironment('test');

// Use Reflection so that we can access private method
$reflection = new ReflectionClass(Populate::class);
$method = $reflection->getMethod('canBuildOnEnvironment');
$method->setAccessible(true);

// Invoke with null object, as this method is static
$this->assertTrue($method->invoke(null));
}

public function testDenyBuildOnLive(): void
{
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
$kernel->setEnvironment('live');

// Use Reflection so that we can access private method
$reflection = new ReflectionClass(Populate::class);
$method = $reflection->getMethod('canBuildOnEnvironment');
$method->setAccessible(true);

// Invoke with null object, as this method is static
$this->assertFalse($method->invoke(null));
}

public function testAllowBuildOnLive(): void
{
Populate::config()->set('allow_build_on_live', true);

/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
$kernel->setEnvironment('live');

// Use Reflection so that we can access private method
$reflection = new ReflectionClass(Populate::class);
$method = $reflection->getMethod('canBuildOnEnvironment');
$method->setAccessible(true);

// Invoke with null object, as this method is static
$this->assertTrue($method->invoke(null));
}

protected function setUp()
{
parent::setUp();

// Make sure that we store the current environment before we kick off our tests
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);

$this->environment = $kernel->getEnvironment();
}

protected function tearDown()
{
// Make sure we reset the environment mode at the end of the test
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
$kernel->setEnvironment($this->environment);

parent::tearDown();
}
}

0 comments on commit 0c40e4e

Please sign in to comment.