- <?php
- * @file
- * Entity CRUD API tests.
- */
-
- * Tests the basic Entity API.
- */
- class EntityAPITestCase extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('entity', 'entity_test');
- }
-
-
- * Tests basic CRUD functionality of the Entity API.
- */
- function testCRUD() {
- $user1 = $this->backdropCreateUser();
-
-
- $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.');
-
-
- $loaded_entity = entity_test_load($entity->id);
- $this->assertEqual($loaded_entity->id, $entity->id, 'Loaded a single entity by id.');
-
-
- $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.');
-
-
- $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.');
-
-
- $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 caching (and not caching) of entities in the Entity API.
- */
- function testCaching() {
- module_enable(array('entity_caching_test'));
-
- $user1 = $this->backdropCreateUser();
-
-
- $entity = entity_create('test_cacheable', array('name' => 'cache_this', 'uid' => $user1->uid));
- $entity->save();
- $loaded_entity = entity_load('test_cacheable', $entity->id);
- $ids = array($entity->id);
- $data = cache_get_multiple($ids, 'cache_entity_test_cacheable');
- $this->assertTrue(!empty($data), "Entity 'test_cacheable' is cached.");
-
-
- $entity = entity_create('test_disable_cache', array('name' => 'disable_cache', 'uid' => $user1->uid));
- $entity->save();
- $loaded_entity = entity_load('test_disable_cache', $entity->id);
- $ids = array($entity->id);
- $data = cache_get_multiple($ids, 'cache_entity_test_disable_cache');
- $this->assertTrue(empty($data), "Entity 'test_disable_cache' is not cached.");
-
-
- $entity = entity_create('test_multibundle', array('type' => 'docache', 'name' => 'bundle_cached', 'uid' => $user1->uid));
- $entity->save();
- $loaded_entity = entity_load('test_multibundle', $entity->id);
- $ids = array($entity->id);
- $data = cache_get_multiple($ids, 'cache_entity_test_multibundle');
- $this->assertTrue(!empty($data), "Entity 'test_multibundle' of type 'docache' is cached.");
-
-
- $entity = entity_create('test_multibundle', array('type' => 'nocache', 'name' => 'bundle_not_cached', 'uid' => $user1->uid));
- $entity->save();
- $loaded_entity = entity_load('test_multibundle', $entity->id);
- $ids = array($entity->id);
- $data = cache_get_multiple($ids, 'cache_entity_test_multibundle');
- $this->assertTrue(empty($data), "Entity 'test_multibundle' of type 'nocache' is not cached.");
- }
-
-
- * Tests basic View functionality of the Entity API.
- */
- function testEntityView() {
- $user1 = $this->backdropCreateUser();
-
-
- $entity = entity_create('entity_test', array('name' => 'test', 'uid' => $user1->uid));
- $entity->field_test_text[LANGUAGE_NONE][0]['value'] = 'Some random text';
- $entity->save();
-
- $this->backdropGet('test/' . $entity->id());
- $this->assertRaw('<h1 class="page-title">Entity test</h1>');
- $this->assertText('Some random text', 'Found the field text on the rendered entity.');
- }
- }
-
- * Tests Entity API base functionality.
- */
- class EntityAPIInfoTestCase extends BackdropWebTestCase {
-
- * 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.');
-
-
-
- state_set('entity_cache_test_label', 'New label.');
- backdrop_flush_all_caches();
- $info = entity_get_info('entity_cache_test');
- $this->assertEqual($info['label'], 'New label.', 'New label appears in entity info.');
-
-
- 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 = state_get('entity_cache_test');
- $this->assertEqual($info['label'], 'Entity Cache Test', 'Entity info label is correct.');
- $this->assertEqual($info['controller class'], 'DefaultEntityController', 'Entity controller class info is correct.');
- }
- }