From 99e7a4bf00ad3aa20bb8157cb728e1db99370b69 Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink Date: Tue, 17 Sep 2019 09:14:17 +0200 Subject: [PATCH] GlobalSet Element Fixture improvements Adds ability to specify whether to use AR or Element --- .../fixtures/elements/GlobalSetFixture.php | 55 +++++++++++++++---- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/src/test/fixtures/elements/GlobalSetFixture.php b/src/test/fixtures/elements/GlobalSetFixture.php index a573aa33d57..e45c731e591 100644 --- a/src/test/fixtures/elements/GlobalSetFixture.php +++ b/src/test/fixtures/elements/GlobalSetFixture.php @@ -37,21 +37,56 @@ 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) { + 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(); + } + + return; + } + + $this->data = []; + foreach ($this->getData() as $alias => $data) { + // Pass in $data so we get an existing element + $element = $this->getElement($data); + + foreach ($data as $handle => $value) { + $element->$handle = $value; + } + + if (!$this->saveElement($element)) { + $this->getErrors($element); + } + + $this->data[$alias] = array_merge($data, ['id' => $element->id]); + } + } + + /** + * {@inheritdoc} + */ + public function unload(): void + { + if ($this->useActiveRecord) { + parent::unload(); } }