KeyValueContentEntityStorageTest.php 5.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
<?php

/**
 * @file
 * Contains \Drupal\system\Tests\KeyValueStore\KeyValueContentEntityStorageTest.
 */

namespace Drupal\system\Tests\KeyValueStore;

use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\simpletest\DrupalUnitTestBase;

/**
 * Tests content entity CRUD with key value entity storage.
 */
class KeyValueContentEntityStorageTest extends DrupalUnitTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('entity', 'user', 'entity_test', 'keyvalue_test');

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'KeyValueEntityStorage content entity test',
      'description' => 'Tests KeyValueEntityStorage for content entities.',
      'group' => 'Entity API',
    );
  }

  /**
   * Tests CRUD operations.
   */
  function testCRUD() {
    $default_langcode = language_default()->id;
    // Verify default properties on a newly created empty entity.
    $empty = entity_create('entity_test_label');
    $this->assertIdentical($empty->id->value, NULL);
    $this->assertIdentical($empty->name->value, NULL);
    $this->assertTrue($empty->uuid->value);
    $this->assertIdentical($empty->langcode->value, $default_langcode);

    // Verify ConfigEntity properties/methods on the newly created empty entity.
    $this->assertIdentical($empty->isNew(), TRUE);
    $this->assertIdentical($empty->bundle(), 'entity_test_label');
    $this->assertIdentical($empty->id(), NULL);
    $this->assertTrue($empty->uuid());
    $this->assertIdentical($empty->label(), NULL);

    // Verify Entity properties/methods on the newly created empty entity.
    $this->assertIdentical($empty->getEntityTypeId(), 'entity_test_label');
    // The URI can only be checked after saving.
    try {
      $empty->urlInfo();
      $this->fail('EntityMalformedException was thrown.');
    }
    catch (EntityMalformedException $e) {
      $this->pass('EntityMalformedException was thrown.');
    }

    // Verify that an empty entity cannot be saved.
    try {
      $empty->save();
      $this->fail('EntityMalformedException was thrown.');
    }
    catch (EntityMalformedException $e) {
      $this->pass('EntityMalformedException was thrown.');
    }

    // Verify that an entity with an empty ID string is considered empty, too.
    $empty_id = entity_create('entity_test_label', array(
      'id' => '',
    ));
    $this->assertIdentical($empty_id->isNew(), TRUE);
    try {
      $empty_id->save();
      $this->fail('EntityMalformedException was thrown.');
    }
    catch (EntityMalformedException $e) {
      $this->pass('EntityMalformedException was thrown.');
    }

    // Verify properties on a newly created entity.
    $entity_test = entity_create('entity_test_label', $expected = array(
      'id' => $this->randomName(),
      'name' => $this->randomString(),
    ));
    $this->assertIdentical($entity_test->id->value, $expected['id']);
    $this->assertTrue($entity_test->uuid->value);
    $this->assertNotEqual($entity_test->uuid->value, $empty->uuid->value);
    $this->assertIdentical($entity_test->name->value, $expected['name']);
    $this->assertIdentical($entity_test->langcode->value, $default_langcode);

    // Verify methods on the newly created entity.
    $this->assertIdentical($entity_test->isNew(), TRUE);
    $this->assertIdentical($entity_test->id(), $expected['id']);
    $this->assertTrue($entity_test->uuid());
    $expected['uuid'] = $entity_test->uuid();
    $this->assertIdentical($entity_test->label(), $expected['name']);

    // Verify that the entity can be saved.
    try {
      $status = $entity_test->save();
      $this->pass('EntityMalformedException was not thrown.');
    }
    catch (EntityMalformedException $e) {
      $this->fail('EntityMalformedException was not thrown.');
    }

    // Verify that the correct status is returned and properties did not change.
    $this->assertIdentical($status, SAVED_NEW);
    $this->assertIdentical($entity_test->id(), $expected['id']);
    $this->assertIdentical($entity_test->uuid(), $expected['uuid']);
    $this->assertIdentical($entity_test->label(), $expected['name']);
    $this->assertIdentical($entity_test->isNew(), FALSE);

    // Save again, and verify correct status and properties again.
    $status = $entity_test->save();
    $this->assertIdentical($status, SAVED_UPDATED);
    $this->assertIdentical($entity_test->id(), $expected['id']);
    $this->assertIdentical($entity_test->uuid(), $expected['uuid']);
    $this->assertIdentical($entity_test->label(), $expected['name']);
    $this->assertIdentical($entity_test->isNew(), FALSE);

    // Ensure that creating an entity with the same id as an existing one is not
    // possible.
    $same_id = entity_create('entity_test_label', array(
      'id' => $entity_test->id(),
    ));
    $this->assertIdentical($same_id->isNew(), TRUE);
    try {
      $same_id->save();
      $this->fail('Not possible to overwrite an entity entity.');
    } catch (EntityStorageException $e) {
      $this->pass('Not possible to overwrite an entity entity.');
    }

    // Verify that renaming the ID returns correct status and properties.
    $ids = array($expected['id'], 'second_' . $this->randomName(4), 'third_' . $this->randomName(4));
    for ($i = 1; $i < 3; $i++) {
      $old_id = $ids[$i - 1];
      $new_id = $ids[$i];
      // Before renaming, everything should point to the current ID.
      $this->assertIdentical($entity_test->id(), $old_id);

      // Rename.
      $entity_test->id = $new_id;
      $this->assertIdentical($entity_test->id(), $new_id);
      $status = $entity_test->save();
      $this->assertIdentical($status, SAVED_UPDATED);
      $this->assertIdentical($entity_test->isNew(), FALSE);

      // Verify that originalID points to new ID directly after renaming.
      $this->assertIdentical($entity_test->id(), $new_id);
    }
  }

}