Skip to content

Commit

Permalink
Merge pull request #14404 from jeabakker/features
Browse files Browse the repository at this point in the history
chore(seeding): prevent automatic seeding of object properties
  • Loading branch information
jdalsem committed Jul 4, 2023
2 parents ab82dd2 + 8667a25 commit 63f35e6
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
18 changes: 15 additions & 3 deletions engine/classes/Elgg/Traits/Seeding.php
Expand Up @@ -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;
Expand All @@ -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'])) {
Expand Down
78 changes: 78 additions & 0 deletions engine/tests/phpunit/unit/Elgg/Traits/SeedingUnitTest.php
@@ -0,0 +1,78 @@
<?php

namespace Elgg\Traits;

use Elgg\UnitTestCase;

class SeedingUnitTest extends UnitTestCase {

use Seeding {
createObject as createSeededObject;
}

/**
* @dataProvider propertyProvider
*/
public function testCreateObjectTitle($input) {
$object = $this->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'],
];
}
}

0 comments on commit 63f35e6

Please sign in to comment.