summaryrefslogtreecommitdiffstats
path: root/core/modules/entity/tests/entity.test
blob: 35b6ab1c26226e4ed054e7c6a77655ddb21aaeb9 (plain)
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
<?php

/**
 * @file
 * Entity CRUD API tests.
 */

/**
 * Tests the basic Entity API.
 */
class EntityAPITestCase extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Entity CRUD',
      'description' => 'Tests basic CRUD functionality.',
      'group' => 'Entity API',
    );
  }

  function setUp() {
    parent::setUp('entity', 'entity_test');
  }

  /**
   * Tests basic CRUD functionality of the Entity API.
   */
  function testCRUD() {
    $user1 = $this->drupalCreateUser();

    // Create some test entities.
    $entity = entity_create('entity_test', array('name' => 'test', 'uid' => $user1->uid));
    $entity->save();
    $entity = entity_create('entity_test', array('name' => 'test2', 'uid' => $user1->uid));
    $entity->save();
    $entity = entity_create('entity_test', array('name' => 'test', 'uid' => NULL));
    $entity->save();

    $entities = array_values(entity_test_load_multiple(FALSE, array('name' => 'test')));

    $this->assertEqual($entities[0]->name, 'test', 'Created and loaded entity.');
    $this->assertEqual($entities[1]->name, 'test', 'Created and loaded entity.');

    // Test loading a single entity.
    $loaded_entity = entity_test_load($entity->id);
    $this->assertEqual($loaded_entity->id, $entity->id, 'Loaded a single entity by id.');

    // Test deleting an entity.
    $entities = array_values(entity_test_load_multiple(FALSE, array('name' => 'test2')));
    $entities[0]->delete();
    $entities = array_values(entity_test_load_multiple(FALSE, array('name' => 'test2')));
    $this->assertEqual($entities, array(), 'Entity deleted.');

    // Test updating an entity.
    $entities = array_values(entity_test_load_multiple(FALSE, array('name' => 'test')));
    $entities[0]->name = 'test3';
    $entities[0]->save();
    $entity = entity_test_load($entities[0]->id);
    $this->assertEqual($entity->name, 'test3', 'Entity updated.');

    // Try deleting multiple test entities by deleting all.
    $ids = array_keys(entity_test_load_multiple(FALSE));
    entity_test_delete_multiple($ids);

    $all = entity_test_load_multiple(FALSE);
    $this->assertTrue(empty($all), 'Deleted all entities.');
  }
}

/**
 * Tests Entity API base functionality.
 */
class EntityAPIInfoTestCase extends DrupalWebTestCase  {

  public static function getInfo() {
    return array(
      'name' => 'Entity info',
      'description' => 'Makes sure entity info is accurately cached.',
      'group' => 'Entity API',
    );
  }

  /**
   * Ensures entity info cache is updated after changes.
   */
  function testEntityInfoChanges() {
    module_enable(array('entity_cache_test'));
    $entity_info = entity_get_info();
    $this->assertTrue(isset($entity_info['entity_cache_test']), 'Test entity type found.');

    // Change the label of the test entity type and make sure changes appear
    // after flushing caches.
    variable_set('entity_cache_test_label', 'New label.');
    drupal_flush_all_caches();
    $info = entity_get_info('entity_cache_test');
    $this->assertEqual($info['label'], 'New label.', 'New label appears in entity info.');

    // Disable the providing module and make sure the entity type is gone.
    module_disable(array('entity_cache_test', 'entity_cache_test_dependency'));
    $entity_info = entity_get_info();
    $this->assertFalse(isset($entity_info['entity_cache_test']), 'Entity type of the providing module is gone.');
  }

  /**
   * Tests entity info cache after enabling a module with a dependency on an entity providing module.
   *
   * @see entity_cache_test_watchdog()
   */
  function testEntityInfoCacheWatchdog() {
    module_enable(array('entity_cache_test'));
    $info = variable_get('entity_cache_test');
    $this->assertEqual($info['label'], 'Entity Cache Test', 'Entity info label is correct.');
    $this->assertEqual($info['controller class'], 'DrupalDefaultEntityController', 'Entity controller class info is correct.');
  }
}