Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GlobalSet Element Fixture improvements #4947

Merged
merged 7 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Added
- Added the option to automatically apply all migrations in the `CRAFT_MIGRATIONS_PATH` when setting up the test environment. ([#4904](https://github.com/craftcms/cms/issues/4904))
- The Control Panel is now translated into Persian. ([#4969)(https://github.com/craftcms/cms/pull/4969))
- Added an option to `craft\test\fixtures\elements\GlobalSetFixture` to specify whether it should load the Active Record instance. ([#4947](https://github.com/craftcms/cms/pull/4947))
- Added the `craft\test\fixtures\elements\ElementFixture::$unload` option to specify whether fixture data should be unloaded.

### Changed
- Any migrations applied during testing will now be stored in the database as content migrations.
Expand Down
6 changes: 6 additions & 0 deletions docs/testing/testing-craft/fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ return [

The primary keys are: `handle`.

::: tip
Global set's do not get their own database row by default.
If you need the Global Sets to have their own database row you can set
the `$useActiveRecord` to true.
:::

### `Tag fixtures`

Extend `craft\test\fixtures\elements\TagFixture` to add tags.
Expand Down
21 changes: 15 additions & 6 deletions src/test/fixtures/elements/ElementFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ abstract class ElementFixture extends ActiveFixture
*/
protected $siteIds = [];

/**
* Whether the fixture data should be unloaded
*
* @var bool
*/
public $unload = true;

// Public Methods
// =========================================================================

Expand Down Expand Up @@ -139,15 +146,17 @@ public function load()
*/
public function unload()
{
foreach ($this->getData() as $data) {
$element = $this->getElement($data);
if ($this->unload) {
foreach ($this->getData() as $data) {
$element = $this->getElement($data);

if ($element && !Craft::$app->getElements()->deleteElement($element, true)) {
throw new InvalidElementException($element, 'Unable to delete element.');
if ($element && !Craft::$app->getElements()->deleteElement($element, true)) {
throw new InvalidElementException($element, 'Unable to delete element.');
}
}
}

$this->data = [];
$this->data = [];
}
}

/**
Expand Down
23 changes: 15 additions & 8 deletions src/test/fixtures/elements/GlobalSetFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,28 @@ abstract class GlobalSetFixture extends ElementFixture
*/
public $tableName = Table::GLOBALSETS;

/**
* @var boolean
*/
public $useActiveRecord = true;

/**
* @inheritdoc
*/
public function load()
{
parent::load();

// TODO: layouts?
foreach ($this->data as $alias => $data) {
$record = new GlobalSetRecord();
$record->id = $data['id'];
$record->name = $data['name'];
$record->handle = $data['handle'];
$record->uid = $data['uid'];
$record->save();
if ($this->useActiveRecord) {
// TODO: layouts?
foreach ($this->data as $alias => $data) {
$record = new GlobalSetRecord();
$record->id = $data['id'];
$record->name = $data['name'];
$record->handle = $data['handle'];
$record->uid = $data['uid'];
$record->save();
}
}
}

Expand Down