View source
<?php
namespace Drupal\config\Tests;
use Drupal\simpletest\KernelTestBase;
class ConfigEntityUnitTest extends KernelTestBase {
protected $strictConfigSchema = FALSE;
public static $modules = array(
'config_test',
);
protected $storage;
protected function setUp() {
parent::setUp();
$this->storage = $this->container
->get('entity.manager')
->getStorage('config_test');
}
public function testStorageMethods() {
$entity_type = \Drupal::entityManager()
->getDefinition('config_test');
$expected_id = 'test_id';
$config_name = $entity_type
->getConfigPrefix() . '.' . $expected_id;
$storage = $this->storage;
$this
->assertIdentical($storage::getIDFromConfigName($config_name, $entity_type
->getConfigPrefix()), $expected_id);
$style = $this
->randomMachineName(8);
for ($i = 0; $i < 2; $i++) {
$entity = $this->storage
->create(array(
'id' => $this
->randomMachineName(),
'label' => $this
->randomString(),
'style' => $style,
));
$entity
->save();
}
$entity = $this->storage
->create(array(
'id' => $this
->randomMachineName(),
'label' => $this
->randomString(),
'style' => $this
->randomMachineName(9),
));
$entity
->save();
$entity_loaded_by_uuid = \Drupal::entityManager()
->loadEntityByUuid($entity_type
->id(), $entity
->uuid());
if (!$entity_loaded_by_uuid) {
$this
->fail(sprintf("Failed to load '%s' entity ID '%s' by UUID '%s'.", $entity_type
->id(), $entity
->id(), $entity
->uuid()));
}
$this
->assertIdentical($entity
->uuid(), $entity_loaded_by_uuid
->uuid());
$entities = $this->storage
->loadByProperties();
$this
->assertEqual(count($entities), 3, 'Three entities are loaded when no properties are specified.');
$entities = $this->storage
->loadByProperties(array(
'style' => $style,
));
$this
->assertEqual(count($entities), 2, 'Two entities are loaded when the style property is specified.');
foreach ($entities as $entity) {
$this
->assertIdentical($entity
->get('style'), $style, 'The loaded entity has the correct style value specified.');
}
$entity = $this->storage
->create(array(
'id' => $this
->randomMachineName(),
'label' => $this
->randomString(),
'style' => 999,
));
$entity
->save();
$this
->assertIdentical('999', $entity->style);
$entity->style = 999;
$entity
->trustData()
->save();
$this
->assertIdentical(999, $entity->style);
$entity
->save();
$this
->assertIdentical('999', $entity->style);
}
}