entity_crud.test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. public function testEntityLoadIds() {
  43. $this->drupalCreateNode(array('title' => 'Node 1'));
  44. $this->drupalCreateNode(array('title' => 'Node 2'));
  45. $nodes_loaded = entity_load('node', array('1', '2'));
  46. $this->assertEqual(count($nodes_loaded), 2);
  47. // Ensure that an id with a trailing decimal place is ignored.
  48. $nodes_loaded = entity_load('node', array('1.', '2'));
  49. $this->assertEqual(count($nodes_loaded), 1);
  50. }
  51. /**
  52. * Tests the controller class loading functionality on non-existing entity
  53. * types and on entities without valid controller class.
  54. */
  55. public function testEntityLoadInvalidControllerClass() {
  56. // Ensure that loading a non-existing entity type will throw an
  57. // EntityMalformedException.
  58. try {
  59. entity_load('test', array('1'));
  60. $this->fail(t('Cannot load a controller class on non-existing entity type.'));
  61. }
  62. catch (EntityMalformedException $e) {
  63. $this->pass(t('Cannot load a controller class on non-existing entity type.'));
  64. }
  65. // Ensure that loading an entity without valid controller class will throw
  66. // an EntityMalformedException.
  67. module_enable(array('entity_crud_hook_test'));
  68. variable_set('entity_crud_hook_test_alter_controller_class', TRUE);
  69. try {
  70. entity_load('node', array('1'));
  71. $this->fail(t('Cannot load a missing or non-existent controller class.'));
  72. }
  73. catch (EntityMalformedException $e) {
  74. $this->pass(t('Cannot load a missing or non-existent controller class.'));
  75. }
  76. variable_set('entity_crud_hook_test_alter_controller_class', FALSE);
  77. }
  78. }