references.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * @file
  4. * Functional tests for the Unique Field module with References field types.
  5. */
  6. class UniqueFieldReferencesTestCase extends DrupalWebTestCase {
  7. protected $privileged_user;
  8. public static function getInfo() {
  9. return array(
  10. 'name' => 'Unique Field: References module tests',
  11. 'description' => 'Ensure that the Unique Field module functions properly with References field types.',
  12. 'group' => 'Unique Field',
  13. );
  14. }
  15. public function setUp() {
  16. parent::setUp('field', 'field_ui', 'options', 'node_reference', 'user_reference', 'unique_field');
  17. // Create and log in our privileged user.
  18. $this->privileged_user = $this->drupalCreateUser(array(
  19. 'administer content types', 'administer nodes', 'bypass node access', 'unique_field_perm_admin', 'unique_field_perm_bypass',
  20. ));
  21. $this->drupalLogin($this->privileged_user);
  22. }
  23. /**
  24. * Test the unique requirement on a node reference field in the content type
  25. * scope.
  26. */
  27. public function testUniqueCtypeNode() {
  28. // Create a content type with a node reference field that is set to be
  29. // unique
  30. $edit = array();
  31. $edit['name'] = 'Unique Node';
  32. $edit['type'] = 'uf_node';
  33. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  34. $this->assertText('The content type Unique Node has been added.', 'Content type added.');
  35. $edit = array();
  36. $edit['fields[_add_new_field][label]'] = 'Unique Node Node';
  37. $edit['fields[_add_new_field][field_name]'] = 'uf_node_node';
  38. $edit['fields[_add_new_field][type]'] = 'node_reference';
  39. $edit['fields[_add_new_field][widget_type]'] = 'options_select';
  40. $this->drupalPost('admin/structure/types/manage/uf_node/fields', $edit, t('Save'));
  41. $this->assertText('These settings apply to the Unique Node Node field everywhere it is used.', 'Field added to content type.');
  42. $edit = array();
  43. $edit['field[settings][referenceable_types][uf_node]'] = 'uf_node';
  44. $this->drupalPost('admin/structure/types/manage/uf_node/fields/field_uf_node_node/field-settings', $edit, t('Save field settings'));
  45. $this->assertText('Updated field Unique Node Node field settings.', 'Node reference field configured.');
  46. $edit = array();
  47. $edit['unique_field_fields[field_uf_node_node]'] = 'field_uf_node_node';
  48. $this->drupalPost('admin/structure/types/manage/uf_node', $edit, t('Save content type'));
  49. $this->assertText('The content type Unique Node has been updated.', 'Content type updated.');
  50. // Create a node to be referenced
  51. $edit = array();
  52. $edit['title'] = $this->randomName(24);
  53. $edit['body[und][0][value]'] = $this->randomName(48);
  54. $this->drupalPost('node/add/uf-node', $edit, t('Save'));
  55. $this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
  56. // Attempt to create 2 nodes that reference the same node
  57. $ref_nid = 1;
  58. $edit = array();
  59. $edit['title'] = $this->randomName(24);
  60. $edit['body[und][0][value]'] = $this->randomName(48);
  61. $edit['field_uf_node_node[und]'] = $ref_nid;
  62. $this->drupalPost('node/add/uf-node', $edit, t('Save'));
  63. $this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
  64. $edit['body[und][0][value]'] = $this->randomName(48);
  65. $this->drupalPost('node/add/uf-node', $edit, t('Save'));
  66. $this->assertText('The Unique Node Node field requires a unique value, and the specified value is already used', 'Unique Node (uf_node) node with duplicate content could not be created');
  67. // Check for false negative: Attempt to create a node with a unique text
  68. $ref_nid = 2;
  69. $edit = array();
  70. $edit['title'] = $this->randomName(24);
  71. $edit['body[und][0][value]'] = $this->randomName(48);
  72. $edit['field_uf_node_node[und]'] = $ref_nid;
  73. $this->drupalPost('node/add/uf-node', $edit, t('Save'));
  74. $this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
  75. }
  76. /**
  77. * Test the unique requirement on a node reference field in the all scope.
  78. */
  79. public function testUniqueAllNode() {
  80. // Create a content type with a node reference field that is set to be
  81. // unique
  82. $edit = array();
  83. $edit['name'] = 'Unique Node';
  84. $edit['type'] = 'uf_node';
  85. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  86. $this->assertText('The content type Unique Node has been added.', 'Content type added.');
  87. $edit = array();
  88. $edit['fields[_add_new_field][label]'] = 'Unique Node Node';
  89. $edit['fields[_add_new_field][field_name]'] = 'uf_node_node';
  90. $edit['fields[_add_new_field][type]'] = 'node_reference';
  91. $edit['fields[_add_new_field][widget_type]'] = 'options_select';
  92. $this->drupalPost('admin/structure/types/manage/uf_node/fields', $edit, t('Save'));
  93. $this->assertText('These settings apply to the Unique Node Node field everywhere it is used.', 'Field added to content type.');
  94. $edit = array();
  95. $edit['field[settings][referenceable_types][uf_node]'] = 'uf_node';
  96. $this->drupalPost('admin/structure/types/manage/uf_node/fields/field_uf_node_node/field-settings', $edit, t('Save field settings'));
  97. $this->assertText('Updated field Unique Node Node field settings.', 'Node reference field configured.');
  98. $edit = array();
  99. $edit['unique_field_fields[field_uf_node_node]'] = 'field_uf_node_node';
  100. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  101. $this->drupalPost('admin/structure/types/manage/uf_node', $edit, t('Save content type'));
  102. $this->assertText('The content type Unique Node has been updated.', 'Content type updated.');
  103. // Create another content type with the same text field and set it to unique
  104. $edit = array();
  105. $edit['name'] = 'Unique Node 2';
  106. $edit['type'] = 'uf_node2';
  107. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  108. $this->assertText('The content type Unique Node 2 has been added.', 'Content type added.');
  109. $edit = array();
  110. $edit['fields[_add_existing_field][label]'] = 'Unique Node 2 Node';
  111. $edit['fields[_add_existing_field][field_name]'] = 'field_uf_node_node';
  112. $edit['fields[_add_existing_field][widget_type]'] = 'options_select';
  113. $this->drupalPost('admin/structure/types/manage/uf_node2/fields', $edit, t('Save'));
  114. $this->assertText('These settings apply only to the Unique Node 2 Node field when used in the Unique Node 2 type.', 'Field added to content type.');
  115. $edit = array();
  116. $edit['field[settings][referenceable_types][uf_node]'] = 'uf_node';
  117. $this->drupalPost('admin/structure/types/manage/uf_node2/fields/field_uf_node_node/field-settings', $edit, t('Save field settings'));
  118. $this->assertText('Updated field Unique Node 2 Node field settings.', 'Node reference field configured.');
  119. $edit = array();
  120. $edit['unique_field_fields[field_uf_node_node]'] = 'field_uf_node_node';
  121. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  122. $this->drupalPost('admin/structure/types/manage/uf_node2', $edit, t('Save content type'));
  123. $this->assertText('The content type Unique Node 2 has been updated.', 'Content type updated.');
  124. // Create a node to be referenced
  125. $edit = array();
  126. $edit['title'] = $this->randomName(24);
  127. $edit['body[und][0][value]'] = $this->randomName(48);
  128. $this->drupalPost('node/add/uf-node', $edit, t('Save'));
  129. $this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
  130. // Attempt to create 2 nodes that reference the same node in different
  131. // content types
  132. $ref_nid = 1;
  133. $edit = array();
  134. $edit['title'] = $this->randomName(24);
  135. $edit['body[und][0][value]'] = $this->randomName(48);
  136. $edit['field_uf_node_node[und]'] = $ref_nid;
  137. $this->drupalPost('node/add/uf-node', $edit, t('Save'));
  138. $this->assertText($edit['body[und][0][value]'], 'Unique Node (uf_node) node has been created');
  139. $edit['body[und][0][value]'] = $this->randomName(48);
  140. $this->drupalPost('node/add/uf-node2', $edit, t('Save'));
  141. $this->assertText('The Unique Node 2 Node field requires a unique value, and the specified value is already used', 'Unique Node 2 (uf_node2) node with duplicate content could not be created');
  142. // Check for false negative: Attempt to create a node with a unique text
  143. $ref_nid = 2;
  144. $edit = array();
  145. $edit['title'] = $this->randomName(24);
  146. $edit['body[und][0][value]'] = $this->randomName(48);
  147. $edit['field_uf_node_node[und]'] = $ref_nid;
  148. $this->drupalPost('node/add/uf-node2', $edit, t('Save'));
  149. $this->assertText($edit['body[und][0][value]'], 'Unique Node 2 (uf_node2) node has been created');
  150. }
  151. /**
  152. * Test the unique requirement on a node reference field in the single scope.
  153. */
  154. public function testUniqueSingleNode() {
  155. // Create a content type with two node reference fields that are set to be
  156. // unique
  157. $edit = array();
  158. $edit['name'] = 'Unique Node Single';
  159. $edit['type'] = 'uf_node_single';
  160. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  161. $this->assertText('The content type Unique Node Single has been added.', 'Content type added.');
  162. $edit = array();
  163. $edit['fields[_add_new_field][label]'] = 'Unique Node Single Node 1';
  164. $edit['fields[_add_new_field][field_name]'] = 'uf_node_single_node_1';
  165. $edit['fields[_add_new_field][type]'] = 'node_reference';
  166. $edit['fields[_add_new_field][widget_type]'] = 'options_select';
  167. $this->drupalPost('admin/structure/types/manage/uf_node_single/fields', $edit, t('Save'));
  168. $this->assertText('These settings apply to the Unique Node Single Node 1 field everywhere it is used.', 'Field added to content type.');
  169. $edit = array();
  170. $edit['field[settings][referenceable_types][uf_node_single]'] = 'uf_node_single';
  171. $this->drupalPost('admin/structure/types/manage/uf_node_single/fields/field_uf_node_single_node_1/field-settings', $edit, t('Save field settings'));
  172. $this->assertText('Updated field Unique Node Single Node 1 field settings.', 'Node reference field configured.');
  173. $edit = array();
  174. $edit['fields[_add_new_field][label]'] = 'Unique Node Single Node 2';
  175. $edit['fields[_add_new_field][field_name]'] = 'uf_node_single_node_2';
  176. $edit['fields[_add_new_field][type]'] = 'node_reference';
  177. $edit['fields[_add_new_field][widget_type]'] = 'options_select';
  178. $this->drupalPost('admin/structure/types/manage/uf_node_single/fields', $edit, t('Save'));
  179. $this->assertText('These settings apply to the Unique Node Single Node 2 field everywhere it is used.', 'Field added to content type.');
  180. $edit = array();
  181. $edit['field[settings][referenceable_types][uf_node_single]'] = 'uf_node_single';
  182. $this->drupalPost('admin/structure/types/manage/uf_node_single/fields/field_uf_node_single_node_2/field-settings', $edit, t('Save field settings'));
  183. $this->assertText('Updated field Unique Node Single Node 2 field settings.', 'Node reference field configured.');
  184. $edit = array();
  185. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_NODE;
  186. $edit['unique_field_fields[field_uf_node_single_node_1]'] = 'field_uf_node_single_node_1';
  187. $edit['unique_field_fields[field_uf_node_single_node_2]'] = 'field_uf_node_single_node_2';
  188. $this->drupalPost('admin/structure/types/manage/uf_node_single', $edit, t('Save content type'));
  189. $this->assertText('The content type Unique Node Single has been updated.', 'Content type updated.');
  190. // Create a node to be referenced
  191. $edit = array();
  192. $edit['title'] = $this->randomName(24);
  193. $edit['body[und][0][value]'] = $this->randomName(48);
  194. $this->drupalPost('node/add/uf-node-single', $edit, t('Save'));
  195. $this->assertText($edit['body[und][0][value]'], 'Unique Node Single (uf_node_single) node has been created');
  196. // Attempt to create a node with the same text in both fields
  197. $ref_nid = 1;
  198. $edit = array();
  199. $edit['title'] = $this->randomName(24);
  200. $edit['body[und][0][value]'] = $this->randomName(48);
  201. $edit['field_uf_node_single_node_1[und]'] = $ref_nid;
  202. $edit['field_uf_node_single_node_2[und]'] = $ref_nid;
  203. $this->drupalPost('node/add/uf-node-single', $edit, t('Save'));
  204. $this->assertText('The Unique Node Single Node 2 fields must have unique values. The Unique Node Single Node 2 field has a value that is already used.', 'Unique Node Single (uf_node_single) node with duplicate content could not be created');
  205. // Check for false negative: Attempt to create a node with unique text
  206. $edit = array();
  207. $edit['title'] = $this->randomName(24);
  208. $edit['body[und][0][value]'] = $this->randomName(48);
  209. $this->drupalPost('node/add/uf-node-single', $edit, t('Save'));
  210. $this->assertText($edit['body[und][0][value]'], 'Unique Node Single (uf_node_single) node has been created');
  211. $edit = array();
  212. $edit['title'] = $this->randomName(24);
  213. $edit['body[und][0][value]'] = $this->randomName(48);
  214. $edit['field_uf_node_single_node_1[und]'] = 1;
  215. $edit['field_uf_node_single_node_2[und]'] = 2;
  216. $this->drupalPost('node/add/uf-node-single', $edit, t('Save'));
  217. $this->assertText($edit['body[und][0][value]'], 'Unique Node Single (uf_node_single) node has been created');
  218. }
  219. }