You are here

public function EntityAPITestCase::testExportables in Entity API 7

Test loading entities defined in code.

File

./entity.test, line 250
Entity CRUD API tests.

Class

EntityAPITestCase
Test basic API.

Code

public function testExportables() {
  module_enable(array(
    'entity_feature',
  ));
  $types = entity_load_multiple_by_name('entity_test_type', array(
    'test2',
    'test',
  ));
  $this
    ->assertEqual(array_keys($types), array(
    'test2',
    'test',
  ), 'Entities have been loaded in the order as specified.');
  $this
    ->assertEqual($types['test']->label, 'label', 'Default type loaded.');
  $this
    ->assertTrue($types['test']->status & ENTITY_IN_CODE && !($types['test']->status & ENTITY_CUSTOM), 'Default type status is correct.');

  // Test using a condition, which has to be applied on the defaults.
  $types = entity_load_multiple_by_name('entity_test_type', FALSE, array(
    'label' => 'label',
  ));
  $this
    ->assertEqual($types['test']->label, 'label', 'Condition to default type applied.');
  $types['test']->label = 'modified';
  $types['test']
    ->save();

  // Ensure loading the changed entity works.
  $types = entity_load_multiple_by_name('entity_test_type', FALSE, array(
    'label' => 'modified',
  ));
  $this
    ->assertEqual($types['test']->label, 'modified', 'Modified type loaded.');

  // Clear the cache to simulate a new page load.
  entity_get_controller('entity_test_type')
    ->resetCache();

  // Test loading using a condition again, now they default may not appear any
  // more as it's overridden by an entity with another label.
  $types = entity_load_multiple_by_name('entity_test_type', FALSE, array(
    'label' => 'label',
  ));
  $this
    ->assertTrue(empty($types), 'Conditions are applied to the overridden entity only.');

  // But the overridden entity has to appear with another condition.
  $types = entity_load_multiple_by_name('entity_test_type', FALSE, array(
    'label' => 'modified',
  ));
  $this
    ->assertEqual($types['test']->label, 'modified', 'Modified default type loaded by condition.');
  $types = entity_load_multiple_by_name('entity_test_type', array(
    'test',
    'test2',
  ));
  $this
    ->assertEqual($types['test']->label, 'modified', 'Modified default type loaded by id.');
  $this
    ->assertTrue(entity_has_status('entity_test_type', $types['test'], ENTITY_OVERRIDDEN), 'Status of overridden type is correct.');

  // Test rebuilding the defaults and make sure overridden entities stay.
  entity_defaults_rebuild();
  $types = entity_load_multiple_by_name('entity_test_type', array(
    'test',
    'test2',
  ));
  $this
    ->assertEqual($types['test']->label, 'modified', 'Overridden entity is still overridden.');
  $this
    ->assertTrue(entity_has_status('entity_test_type', $types['test'], ENTITY_OVERRIDDEN), 'Status of overridden type is correct.');

  // Test reverting.
  $types['test']
    ->delete();
  $types = entity_load_multiple_by_name('entity_test_type', array(
    'test',
    'test2',
  ));
  $this
    ->assertEqual($types['test']->label, 'label', 'Entity has been reverted.');

  // Test loading an exportable by its numeric id.
  $result = entity_load_multiple_by_name('entity_test_type', array(
    $types['test']->id,
  ));
  $this
    ->assertTrue(isset($result['test']), 'Exportable entity loaded by the numeric id.');

  // Test exporting an entity to JSON.
  $serialized_string = $types['test']
    ->export();
  $data = drupal_json_decode($serialized_string);
  $this
    ->assertNotNull($data, 'Exported entity is valid JSON.');
  $import = entity_import('entity_test_type', $serialized_string);
  $this
    ->assertTrue(get_class($import) == get_class($types['test']) && $types['test']->label == $import->label, 'Successfully exported entity to code.');
  $this
    ->assertTrue(!isset($import->status), 'Exportable status has not been exported to code.');

  // Test disabling the module providing the defaults in code.
  $types = entity_load_multiple_by_name('entity_test_type', array(
    'test',
    'test2',
  ));
  $types['test']->label = 'modified';
  $types['test']
    ->save();
  module_disable(array(
    'entity_feature',
  ));

  // Make sure the overridden entity stays and the other one is deleted.
  entity_get_controller('entity_test_type')
    ->resetCache();
  $test = entity_load_single('entity_test_type', 'test');
  $this
    ->assertTrue(!empty($test) && $test->label == 'modified', 'Overidden entity is still available.');
  $this
    ->assertTrue(!empty($test) && !entity_has_status('entity_test_type', $test, ENTITY_IN_CODE) && entity_has_status('entity_test_type', $test, ENTITY_CUSTOM), 'Overidden entity is now marked as custom.');
  $test2 = entity_load_single('entity_test_type', 'test2');
  $this
    ->assertFalse($test2, 'Default entity has disappeared.');
}