entity_crud.test 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Entity CRUD API.
  5. */
  6. /**
  7. * Tests the entity_load() function.
  8. */
  9. class EntityLoadTestCase extends DrupalWebTestCase {
  10. protected $profile = 'testing';
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Entity loading',
  14. 'description' => 'Tests the entity_load() function.',
  15. 'group' => 'Entity API',
  16. );
  17. }
  18. /**
  19. * Tests the functionality for loading entities matching certain conditions.
  20. */
  21. public function testEntityLoadConditions() {
  22. // Create a few nodes. One of them is given an edge-case title of "Array",
  23. // because loading entities by an array of conditions is subject to PHP
  24. // array-to-string conversion issues and we want to test those.
  25. $node_1 = $this->drupalCreateNode(array('title' => 'Array'));
  26. $node_2 = $this->drupalCreateNode(array('title' => 'Node 2'));
  27. $node_3 = $this->drupalCreateNode(array('title' => 'Node 3'));
  28. // Load all entities so that they are statically cached.
  29. $all_nodes = entity_load('node', FALSE);
  30. // Check that the first node can be loaded by title.
  31. $nodes_loaded = entity_load('node', FALSE, array('title' => 'Array'));
  32. $this->assertEqual(array_keys($nodes_loaded), array($node_1->nid));
  33. // Check that the second and third nodes can be loaded by title using an
  34. // array of conditions, and that the first node is not loaded from the
  35. // cache along with them.
  36. $nodes_loaded = entity_load('node', FALSE, array('title' => array('Node 2', 'Node 3')));
  37. ksort($nodes_loaded);
  38. $this->assertEqual(array_keys($nodes_loaded), array($node_2->nid, $node_3->nid));
  39. $this->assertIdentical($nodes_loaded[$node_2->nid], $all_nodes[$node_2->nid], 'Loaded node 2 is identical to cached node.');
  40. $this->assertIdentical($nodes_loaded[$node_3->nid], $all_nodes[$node_3->nid], 'Loaded node 3 is identical to cached node.');
  41. }
  42. }