entity_crud.test

Tests for the Entity CRUD API.

File

modules/simpletest/tests/entity_crud.test

View source
<?php


/**
 * @file
 * Tests for the Entity CRUD API.
 */


/**
 * Tests the entity_load() function.
 */
class EntityLoadTestCase extends DrupalWebTestCase {
  protected $profile = 'testing';
  public static function getInfo() {
    return array(
      'name' => 'Entity loading',
      'description' => 'Tests the entity_load() function.',
      'group' => 'Entity API',
    );
  }
  
  /**
   * Tests the functionality for loading entities matching certain conditions.
   */
  public function testEntityLoadConditions() {
    // Create a few nodes. One of them is given an edge-case title of "Array",
    // because loading entities by an array of conditions is subject to PHP
    // array-to-string conversion issues and we want to test those.
    $node_1 = $this->drupalCreateNode(array(
      'title' => 'Array',
    ));
    $node_2 = $this->drupalCreateNode(array(
      'title' => 'Node 2',
    ));
    $node_3 = $this->drupalCreateNode(array(
      'title' => 'Node 3',
    ));
    // Load all entities so that they are statically cached.
    $all_nodes = entity_load('node', FALSE);
    // Check that the first node can be loaded by title.
    $nodes_loaded = entity_load('node', FALSE, array(
      'title' => 'Array',
    ));
    $this->assertEqual(array_keys($nodes_loaded), array(
      $node_1->nid,
    ));
    // Check that the second and third nodes can be loaded by title using an
    // array of conditions, and that the first node is not loaded from the
    // cache along with them.
    $nodes_loaded = entity_load('node', FALSE, array(
      'title' => array(
        'Node 2',
        'Node 3',
      ),
    ));
    ksort($nodes_loaded);
    $this->assertEqual(array_keys($nodes_loaded), array(
      $node_2->nid,
      $node_3->nid,
    ));
    $this->assertIdentical($nodes_loaded[$node_2->nid], $all_nodes[$node_2->nid], 'Loaded node 2 is identical to cached node.');
    $this->assertIdentical($nodes_loaded[$node_3->nid], $all_nodes[$node_3->nid], 'Loaded node 3 is identical to cached node.');
  }
  public function testEntityLoadIds() {
    $this->drupalCreateNode(array(
      'title' => 'Node 1',
    ));
    $this->drupalCreateNode(array(
      'title' => 'Node 2',
    ));
    $nodes_loaded = entity_load('node', array(
      '1',
      '2',
    ));
    $this->assertEqual(count($nodes_loaded), 2);
    // Ensure that an id with a trailing decimal place is ignored.
    $nodes_loaded = entity_load('node', array(
      '1.',
      '2',
    ));
    $this->assertEqual(count($nodes_loaded), 1);
  }
  
  /**
   * Tests the controller class loading functionality on non-existing entity
   * types and on entities without valid controller class.
   */
  public function testEntityLoadInvalidControllerClass() {
    // Ensure that loading a non-existing entity type will throw an
    // EntityMalformedException.
    try {
      entity_load('test', array(
        '1',
      ));
      $this->fail(t('Cannot load a controller class on non-existing entity type.'));
    } catch (EntityMalformedException $e) {
      $this->pass(t('Cannot load a controller class on non-existing entity type.'));
    }
    // Ensure that loading an entity without valid controller class will throw
    // an EntityMalformedException.
    module_enable(array(
      'entity_crud_hook_test',
    ));
    variable_set('entity_crud_hook_test_alter_controller_class', TRUE);
    try {
      entity_load('node', array(
        '1',
      ));
      $this->fail(t('Cannot load a missing or non-existent controller class.'));
    } catch (EntityMalformedException $e) {
      $this->pass(t('Cannot load a missing or non-existent controller class.'));
    }
    variable_set('entity_crud_hook_test_alter_controller_class', FALSE);
  }

}

Classes

Title Deprecated Summary
EntityLoadTestCase Tests the entity_load() function.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.