CerBasicTestCase.test 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. class CerBasicTestCase extends DrupalWebTestCase {
  3. /**
  4. * A user account under which the tests will be conducted.
  5. *
  6. * @var StdClass
  7. */
  8. protected $author;
  9. /**
  10. * A node that references $this->author.
  11. *
  12. * @var StdClass
  13. */
  14. protected $node;
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'Basic',
  18. 'description' => "Tests CER's basic CRUD functionality.",
  19. 'group' => 'CER',
  20. );
  21. }
  22. public function setUp() {
  23. // Turns out you have to enable your own module in the friggin' test, if
  24. // you want your own classes to be available. Lawsy me and other such
  25. // utterances...SimpleTest is teh suck.
  26. parent::setUp('entity', 'ctools', 'entityreference', 'field_object', 'cer');
  27. $field = array(
  28. 'type' => 'entityreference',
  29. 'cardinality' => 1,
  30. 'field_name' => 'field_author',
  31. 'settings' => array(
  32. 'target_type' => 'user',
  33. ),
  34. );
  35. field_create_field($field);
  36. $instance = array(
  37. 'field_name' => $field['field_name'],
  38. 'entity_type' => 'node',
  39. 'bundle' => 'page',
  40. );
  41. field_create_instance($instance);
  42. $field = array(
  43. 'type' => 'entityreference',
  44. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  45. 'field_name' => 'field_pages',
  46. 'settings' => array(
  47. 'target_type' => 'node',
  48. ),
  49. 'handler_settings' => array(
  50. 'target_bundles' => array('page'),
  51. ),
  52. );
  53. field_create_field($field);
  54. $instance = array(
  55. 'field_name' => $field['field_name'],
  56. 'entity_type' => 'user',
  57. 'bundle' => 'user',
  58. );
  59. field_create_instance($instance);
  60. /** @var CerPreset $preset */
  61. $preset = entity_create('cer', array());
  62. $preset->wrapper->cer_left->set('node:page:field_author');
  63. $preset->wrapper->cer_right->set('user:user:field_pages');
  64. $preset->wrapper->cer_bidirectional->set(TRUE);
  65. $preset->wrapper->cer_enabled->set(TRUE);
  66. $preset->save();
  67. $this->author = $this->drupalCreateUser();
  68. $this->node = $this->drupalCreateNode(array(
  69. 'field_author' => array(
  70. LANGUAGE_NONE => array(
  71. array('target_id' => $this->author->uid),
  72. ),
  73. ),
  74. ));
  75. }
  76. public function testCreate() {
  77. $author = $this->reload('user', $this->author);
  78. $this->assertEqual(1, $author->field_pages->count());
  79. $this->assertEqual($this->node->nid, $author->field_pages[0]->nid->value());
  80. }
  81. public function testUpdateDereference() {
  82. // Dereference the node from the user.
  83. $this->author->field_pages = array();
  84. user_save($this->author);
  85. // Reload $this->node from the database so we get the latest field values.
  86. $this->assertNull($this->reload('node', $this->node)->field_author->value());
  87. }
  88. public function testUpdateChangeTarget() {
  89. $target = $this->drupalCreateUser();
  90. $this->node->field_author[LANGUAGE_NONE][0]['target_id'] = $target->uid;
  91. node_save($this->node);
  92. // The original author should no longer reference the node.
  93. $this->assertEqual(0, $this->reload('user', $this->author)->field_pages->count());
  94. /** @var EntityDrupalWrapper $target */
  95. $target = $this->reload('user', $target);
  96. $this->assertEqual(1, $target->field_pages->count());
  97. $this->assertEqual($this->node->nid, $target->field_pages[0]->nid->value());
  98. }
  99. public function testDelete() {
  100. node_delete($this->node->nid);
  101. $this->assertEqual(0, $this->reload('user', $this->author)->field_pages->count());
  102. }
  103. /**
  104. * @return EntityDrupalWrapper
  105. */
  106. protected function reload($entity_type, $entity) {
  107. list ($id) = entity_extract_IDs($entity_type, $entity);
  108. $entities = entity_load($entity_type, array($id), NULL, TRUE);
  109. return entity_metadata_wrapper($entity_type, $entities[$id]);
  110. }
  111. }