diff --git a/engine/classes/Elgg/Traits/Seeding.php b/engine/classes/Elgg/Traits/Seeding.php index 37335ceb9ad..9e1398c50d7 100644 --- a/engine/classes/Elgg/Traits/Seeding.php +++ b/engine/classes/Elgg/Traits/Seeding.php @@ -381,6 +381,12 @@ public function createGroup(array $properties = [], array $options = []): \ElggG * @throws MaxAttemptsException */ public function createObject(array $properties = [], array $options = []): \ElggObject { + $default_properties = [ + 'title' => true, + 'description' => true, + 'tags' => true, + ]; + $properties = array_merge($default_properties, $properties); $create = function () use ($properties, $options) { $properties['__faker'] = true; @@ -389,20 +395,26 @@ public function createObject(array $properties = [], array $options = []): \Elgg $properties['time_created'] = $this->getRandomCreationTimestamp(); } - if (empty($properties['title'])) { + if ($properties['title'] === true) { $properties['title'] = $this->faker()->sentence(); + } elseif ($properties['title'] === false) { + unset($properties['title']); } - if (empty($properties['description'])) { + if ($properties['description'] === true) { $properties['description'] = $this->faker()->text($this->faker()->numberBetween(500, 1000)); + } elseif ($properties['description'] === false) { + unset($properties['description']); } if (empty($properties['subtype'])) { $properties['subtype'] = $this->getRandomSubtype(); } - if (empty($properties['tags'])) { + if ($properties['tags'] === true) { $properties['tags'] = $this->faker()->words(10); + } elseif ($properties['tags'] === false) { + unset($properties['tags']); } if (!isset($properties['owner_guid'])) { diff --git a/engine/tests/phpunit/unit/Elgg/Traits/SeedingUnitTest.php b/engine/tests/phpunit/unit/Elgg/Traits/SeedingUnitTest.php new file mode 100644 index 00000000000..2aa6ea62571 --- /dev/null +++ b/engine/tests/phpunit/unit/Elgg/Traits/SeedingUnitTest.php @@ -0,0 +1,78 @@ +createSeededObject([ + 'title' => $input, + ], ['save' => false]); + $this->assertInstanceOf(\ElggObject::class, $object); + + if ($input === false) { + $this->assertNull($object->title); + } else { + $this->assertIsString($object->title); + } + + if (is_string($input)) { + $this->assertEquals($input, $object->title); + } + } + + /** + * @dataProvider propertyProvider + */ + public function testCreateObjectDescription($input) { + $object = $this->createSeededObject([ + 'description' => $input, + ], ['save' => false]); + $this->assertInstanceOf(\ElggObject::class, $object); + + if ($input === false) { + $this->assertNull($object->description); + } else { + $this->assertIsString($object->description); + } + + if (is_string($input)) { + $this->assertEquals($input, $object->description); + } + } + + /** + * @dataProvider propertyProvider + */ + public function testCreateObjectTags($input) { + $object = $this->createSeededObject([ + 'tags' => $input, + ], ['save' => false]); + $this->assertInstanceOf(\ElggObject::class, $object); + + if ($input === false) { + $this->assertNull($object->tags); + } elseif (is_string($input)) { + $this->assertEquals($input, $object->tags); + } else { + $this->assertIsArray($object->tags); + } + } + + public function propertyProvider() { + return [ + [true], + [false], + ['hello world'], + ]; + } +}