crud.test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. class CerCrudTest extends DrupalWebTestCase {
  3. public static function getInfo() {
  4. return array(
  5. 'name' => 'CRUD',
  6. 'group' => 'Corresponding Entity Reference',
  7. 'description' => 'Tests basic CER functionality.',
  8. );
  9. }
  10. public function setUp() {
  11. parent::setUp('field', 'field_sql_storage', 'ctools', 'entityreference', 'cer');
  12. field_create_field(array(
  13. 'field_name' => 'field_user',
  14. 'type' => 'entityreference',
  15. 'cardinality' => -1,
  16. 'settings' => array(
  17. 'target_type' => 'user',
  18. ),
  19. ));
  20. field_create_field(array(
  21. 'field_name' => 'field_node',
  22. 'type' => 'entityreference',
  23. 'cardinality' => -1,
  24. 'settings' => array(
  25. 'target_type' => 'node',
  26. ),
  27. ));
  28. field_create_instance(array(
  29. 'field_name' => 'field_user',
  30. 'entity_type' => 'node',
  31. 'bundle' => 'page',
  32. ));
  33. field_create_instance(array(
  34. 'field_name' => 'field_node',
  35. 'entity_type' => 'user',
  36. 'bundle' => 'user',
  37. ));
  38. db_insert('cer')->fields(array(
  39. 'entity_types_content_fields' => 'node*page*field_user*user*user*field_node',
  40. 'enabled' => TRUE,
  41. ))->execute();
  42. }
  43. public function testImplicitReferenceCreation() {
  44. $uid = $this->drupalCreateUser()->uid;
  45. $referrers = array();
  46. for ($i = 0; $i < 5; $i++) {
  47. $referrers[] = $this->drupalCreateNode(array(
  48. 'type' => 'page',
  49. 'field_user' => array(
  50. 'und' => array(
  51. array('target_id' => $uid),
  52. ),
  53. ),
  54. ))->nid;
  55. }
  56. $references = array();
  57. foreach (user_load($uid, TRUE)->field_node['und'] as $reference) {
  58. $references[] = $reference['target_id'];
  59. }
  60. $this->assertFalse(array_diff($referrers, $references), 'Creating 5 referrers to a single entity creates 5 corresponding references on that entity.', 'CER');
  61. }
  62. public function testDuplicateReferencePrevention() {
  63. $uid = $this->drupalCreateUser()->uid;
  64. $this->drupalCreateNode(array(
  65. 'type' => 'page',
  66. 'field_user' => array(
  67. 'und' => array(
  68. array('target_id' => $uid),
  69. array('target_id' => $uid),
  70. ),
  71. ),
  72. ));
  73. $account = user_load($uid, TRUE);
  74. $this->assertEqual(sizeof($account->field_node['und']), 1, 'Creating two references to an entity from a single referrer creates one corresponding reference.', 'CER');
  75. }
  76. public function testExplicitReferenceCreation() {
  77. $uid = $this->drupalCreateNode()->uid;
  78. $node = $this->drupalCreateNode(array('type' => 'page'));
  79. $node->field_user['und'][0]['target_id'] = $uid;
  80. node_save($node);
  81. $account = user_load($uid, TRUE);
  82. $this->assertEqual($account->field_node['und'][0]['target_id'], $node->nid, 'Creating an explicit reference between to unrelated entities creates a corresponding reference.', 'CER');
  83. }
  84. public function testExplicitDereference() {
  85. $uid = $this->drupalCreateUser()->uid;
  86. $nid = $this->drupalCreateNode(array(
  87. 'type' => 'page',
  88. 'field_user' => array(
  89. 'und' => array(
  90. array('target_id' => $uid),
  91. ),
  92. ),
  93. ))->nid;
  94. $account = user_load($uid, TRUE);
  95. $account->field_node = array();
  96. user_save($account);
  97. $node = node_load($nid, NULL, TRUE);
  98. $this->assertFalse($node->field_user, 'Explicitly clearing a reference from the referenced entity clears the corresponding reference on the referrer.', 'CER');
  99. }
  100. public function testReferrerDeletion() {
  101. $uid = $this->drupalCreateUser()->uid;
  102. $referrers = array();
  103. for ($i = 0; $i < 5; $i++) {
  104. $referrers[] = $this->drupalCreateNode(array(
  105. 'type' => 'page',
  106. 'field_user' => array(
  107. 'und' => array(
  108. array('target_id' => $uid),
  109. ),
  110. ),
  111. ))->nid;
  112. }
  113. node_delete($referrers[0]);
  114. $references = array();
  115. foreach (user_load($uid, TRUE)->field_node['und'] as $reference) {
  116. $references[] = $reference['target_id'];
  117. }
  118. $this->assertFalse(in_array($referrers[0], $references), 'Deleting a referrer clears corresponding reference on the referenced entity.', 'CER');
  119. }
  120. public function testReferencedEntityDeletion() {
  121. $uid = $this->drupalCreateUser()->uid;
  122. $referrers = array();
  123. for ($i = 0; $i < 5; $i++) {
  124. $referrers[] = $this->drupalCreateNode(array(
  125. 'type' => 'page',
  126. 'field_user' => array(
  127. 'und' => array(
  128. array('target_id' => $uid),
  129. ),
  130. ),
  131. ))->nid;
  132. }
  133. user_delete($uid);
  134. $cleared = 0;
  135. foreach ($referrers as $nid) {
  136. $node = node_load($nid, NULL, TRUE);
  137. $cleared += (int) empty($node->field_user);
  138. }
  139. $this->assertEqual($cleared, sizeof($referrers), 'Deleting a referenced entity clears all references to it.', 'CER');
  140. }
  141. }