core.test 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. /**
  3. * @file
  4. * Functional tests for the Unique Field module with Drupal core field types.
  5. *
  6. * The basic model for the tests:
  7. *
  8. * - Test each supported field type
  9. * - Test each scope (by content type, language, all, or single node)
  10. * - Test multiple fields individually and in combination
  11. */
  12. class UniqueFieldCoreTestCase extends DrupalWebTestCase {
  13. protected $privileged_user;
  14. public static function getInfo() {
  15. return array(
  16. 'name' => 'Unique Field: Core tests',
  17. 'description' => 'Ensure that the Unique Field module functions properly with Drupal core field types.',
  18. 'group' => 'Unique Field',
  19. );
  20. }
  21. public function setUp() {
  22. parent::setUp('field', 'field_ui', 'number', 'text', 'unique_field');
  23. // TODO: add tests for other types of fields: date, file, link, number, node
  24. // reference, user reference, etc.
  25. // Create and log in our privileged user.
  26. $this->privileged_user = $this->drupalCreateUser(array(
  27. 'administer content types', 'administer nodes', 'bypass node access', 'unique_field_perm_admin', 'unique_field_perm_bypass',
  28. ));
  29. $this->drupalLogin($this->privileged_user);
  30. }
  31. /**
  32. * Test the unique requirement on the title field in the content type scope.
  33. */
  34. public function testUniqueCtypeTitle() {
  35. // Create a content type with the title set to be unique
  36. $edit = array();
  37. $edit['name'] = 'Unique Title';
  38. $edit['type'] = 'uf_title';
  39. $edit['unique_field_fields[title]'] = 'title';
  40. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  41. $this->assertText('The content type Unique Title has been added.', 'Content type added.');
  42. // Attempt to create 2 nodes with the same title
  43. $title = $this->randomName(24);
  44. $edit = array();
  45. $edit['title'] = $title;
  46. $edit['body[und][0][value]'] = $this->randomName(48);
  47. $this->drupalPost('node/add/uf-title', $edit, t('Save'));
  48. $this->assertText($edit['body[und][0][value]'], 'Unique Title (uf_title) node has been created');
  49. $edit['body[und][0][value]'] = $this->randomName(48);
  50. $this->drupalPost('node/add/uf-title', $edit, t('Save'));
  51. $this->assertText('The Title field requires a unique value, and the specified value is already used', 'Unique Title (uf_title) node with duplicate content could not be created');
  52. // Check for false negative: Attempt to create a node with a unique title
  53. $edit = array();
  54. $edit['title'] = $this->randomName(24);
  55. $edit['body[und][0][value]'] = $this->randomName(48);
  56. $this->drupalPost('node/add/uf-title', $edit, t('Save'));
  57. $this->assertText($edit['body[und][0][value]'], 'Unique Title (uf_title) node has been created');
  58. }
  59. /**
  60. * Test the unique requirement on the title field in the all scope.
  61. */
  62. public function testUniqueAllTitle() {
  63. // Create a content type with the title set to be unique
  64. $edit = array();
  65. $edit['name'] = 'Unique Title';
  66. $edit['type'] = 'uf_title';
  67. $edit['unique_field_fields[title]'] = 'title';
  68. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  69. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  70. $this->assertText('The content type Unique Title has been added.', 'Content type added.');
  71. // Create another content type with the title set to be unique
  72. $edit = array();
  73. $edit['name'] = 'Unique Title 2';
  74. $edit['type'] = 'uf_title2';
  75. $edit['unique_field_fields[title]'] = 'title';
  76. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  77. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  78. $this->assertText('The content type Unique Title 2 has been added.', 'Content type added.');
  79. // Attempt to create 2 nodes with the same title in different content types
  80. $title = $this->randomName(24);
  81. $edit = array();
  82. $edit['title'] = $title;
  83. $edit['body[und][0][value]'] = $this->randomName(48);
  84. $this->drupalPost('node/add/uf-title', $edit, t('Save'));
  85. $this->assertText($edit['body[und][0][value]'], 'Unique Title (uf_title) node has been created');
  86. $edit['body[und][0][value]'] = $this->randomName(48);
  87. $this->drupalPost('node/add/uf-title2', $edit, t('Save'));
  88. $this->assertText('The Title field requires a unique value, and the specified value is already used', 'Unique Title (uf_title) node with duplicate content could not be created');
  89. // Check for false negative: Attempt to create a node with a unique title
  90. $edit = array();
  91. $edit['title'] = $this->randomName(24);
  92. $edit['body[und][0][value]'] = $this->randomName(48);
  93. $this->drupalPost('node/add/uf-title2', $edit, t('Save'));
  94. $this->assertText($edit['body[und][0][value]'], 'Unique Title 2 (uf_title2) node has been created');
  95. }
  96. /**
  97. * Test the unique requirement on the node author in the content type scope.
  98. */
  99. public function testUniqueCtypeAuthor() {
  100. // Create a content type with the author set to be unique
  101. $edit = array();
  102. $edit['name'] = 'Unique Author';
  103. $edit['type'] = 'uf_author';
  104. $edit['unique_field_fields[name]'] = 'name';
  105. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  106. $this->assertText('The content type Unique Author has been added.', 'Content type added.');
  107. // Attempt to create 2 nodes with the same author
  108. $edit = array();
  109. $edit['title'] = $this->randomName(24);
  110. $edit['body[und][0][value]'] = $this->randomName(48);
  111. $this->drupalPost('node/add/uf-author', $edit, t('Save'));
  112. $this->assertText($edit['body[und][0][value]'], 'Unique Author (uf_author) node has been created');
  113. $edit = array();
  114. $edit['title'] = $this->randomName(24);
  115. $edit['body[und][0][value]'] = $this->randomName(48);
  116. $this->drupalPost('node/add/uf-author', $edit, t('Save'));
  117. $this->assertText('The Author field requires a unique value, and the specified value is already used', 'Unique Author (uf_author) node with duplicate content could not be created');
  118. // Check for false negative: Attempt to create a node with a unique author
  119. $new_account = $this->drupalCreateUser(array('administer nodes', 'bypass node access'));
  120. $this->drupalLogin($new_account);
  121. $edit = array();
  122. $edit['title'] = $this->randomName(24);
  123. $edit['body[und][0][value]'] = $this->randomName(48);
  124. $this->drupalPost('node/add/uf-author', $edit, t('Save'));
  125. $this->assertText($edit['body[und][0][value]'], 'Unique Author (uf_author) node has been created');
  126. }
  127. /**
  128. * Test the unique requirement on the node author in the all scope.
  129. */
  130. public function testUniqueAllAuthor() {
  131. // Create a content type with the author set to be unique
  132. $edit = array();
  133. $edit['name'] = 'Unique Author';
  134. $edit['type'] = 'uf_author';
  135. $edit['unique_field_fields[name]'] = 'name';
  136. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  137. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  138. $this->assertText('The content type Unique Author has been added.', 'Content type added.');
  139. // Create another content type with the author set to be unique
  140. $edit = array();
  141. $edit['name'] = 'Unique Author 2';
  142. $edit['type'] = 'uf_author2';
  143. $edit['unique_field_fields[name]'] = 'name';
  144. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  145. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  146. $this->assertText('The content type Unique Author 2 has been added.', 'Content type added.');
  147. // Attempt to create 2 nodes with the same author in different content types
  148. $edit = array();
  149. $edit['title'] = $this->randomName(24);
  150. $edit['body[und][0][value]'] = $this->randomName(48);
  151. $this->drupalPost('node/add/uf-author', $edit, t('Save'));
  152. $this->assertText($edit['body[und][0][value]'], 'Unique Author (uf_author) node has been created');
  153. $edit = array();
  154. $edit['title'] = $this->randomName(24);
  155. $edit['body[und][0][value]'] = $this->randomName(48);
  156. $this->drupalPost('node/add/uf-author2', $edit, t('Save'));
  157. $this->assertText('The Author field requires a unique value, and the specified value is already used', 'Unique Author 2 (uf_author2) node with duplicate content could not be created');
  158. // Check for false negative: Attempt to create a node with a unique title
  159. $new_account = $this->drupalCreateUser(array('administer nodes', 'bypass node access'));
  160. $this->drupalLogin($new_account);
  161. $edit = array();
  162. $edit['title'] = $this->randomName(24);
  163. $edit['body[und][0][value]'] = $this->randomName(48);
  164. $this->drupalPost('node/add/uf-author2', $edit, t('Save'));
  165. $this->assertText($edit['body[und][0][value]'], 'Unique Author 2 (uf_author2) node has been created');
  166. }
  167. /**
  168. * Test the unique requirement on a text field in the content type scope.
  169. */
  170. public function testUniqueCtypeText() {
  171. // Create a content type with a text field that is set to be unique
  172. $edit = array();
  173. $edit['name'] = 'Unique Text';
  174. $edit['type'] = 'uf_text';
  175. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  176. $this->assertText('The content type Unique Text has been added.', 'Content type added.');
  177. $edit = array();
  178. $edit['fields[_add_new_field][label]'] = 'Unique Text Text';
  179. $edit['fields[_add_new_field][field_name]'] = 'uf_text_text';
  180. $edit['fields[_add_new_field][type]'] = 'text';
  181. $edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
  182. $this->drupalPost('admin/structure/types/manage/uf_text/fields', $edit, t('Save'));
  183. $this->assertText('These settings apply to the Unique Text Text field everywhere it is used.', 'Field added to content type.');
  184. $edit = array();
  185. $edit['unique_field_fields[field_uf_text_text]'] = 'field_uf_text_text';
  186. $this->drupalPost('admin/structure/types/manage/uf_text', $edit, t('Save content type'));
  187. $this->assertText('The content type Unique Text has been updated.', 'Content type updated.');
  188. // Attempt to create 2 nodes with the same text field value
  189. $text = $this->randomName(48);
  190. $edit = array();
  191. $edit['title'] = $this->randomName(24);
  192. $edit['body[und][0][value]'] = $this->randomName(48);
  193. $edit['field_uf_text_text[und][0][value]'] = $text;
  194. $this->drupalPost('node/add/uf-text', $edit, t('Save'));
  195. $this->assertText($edit['body[und][0][value]'], 'Unique Text (uf_text) node has been created');
  196. $edit['body[und][0][value]'] = $this->randomName(48);
  197. $this->drupalPost('node/add/uf-text', $edit, t('Save'));
  198. $this->assertText('The Unique Text Text field requires a unique value, and the specified value is already used', 'Unique Text (uf_text) node with duplicate content could not be created');
  199. // Check for false negative: Attempt to create a node with a unique text
  200. $edit = array();
  201. $edit['title'] = $this->randomName(24);
  202. $edit['body[und][0][value]'] = $this->randomName(48);
  203. $edit['field_uf_text_text[und][0][value]'] = $this->randomName(48);
  204. $this->drupalPost('node/add/uf-text', $edit, t('Save'));
  205. $this->assertText($edit['body[und][0][value]'], 'Unique Text (uf_text) node has been created');
  206. }
  207. /**
  208. * Test the unique requirement on a text field in the all scope.
  209. */
  210. public function testUniqueAllText() {
  211. // Create a content type with a text field that is set to be unique
  212. $edit = array();
  213. $edit['name'] = 'Unique Text';
  214. $edit['type'] = 'uf_text';
  215. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  216. $this->assertText('The content type Unique Text has been added.', 'Content type added.');
  217. $edit = array();
  218. $edit['fields[_add_new_field][label]'] = 'Unique Text Text';
  219. $edit['fields[_add_new_field][field_name]'] = 'uf_text_text';
  220. $edit['fields[_add_new_field][type]'] = 'text';
  221. $edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
  222. $this->drupalPost('admin/structure/types/manage/uf_text/fields', $edit, t('Save'));
  223. $this->assertText('These settings apply to the Unique Text Text field everywhere it is used.', 'Field added to content type.');
  224. $edit = array();
  225. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  226. $edit['unique_field_fields[field_uf_text_text]'] = 'field_uf_text_text';
  227. $this->drupalPost('admin/structure/types/manage/uf_text', $edit, t('Save content type'));
  228. $this->assertText('The content type Unique Text has been updated.', 'Content type updated.');
  229. // Create another content type with the same text field and set it to unique
  230. $edit = array();
  231. $edit['name'] = 'Unique Text 2';
  232. $edit['type'] = 'uf_text2';
  233. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  234. $this->assertText('The content type Unique Text 2 has been added.', 'Content type added.');
  235. $edit = array();
  236. $edit['fields[_add_existing_field][label]'] = 'Unique Text 2 Text';
  237. $edit['fields[_add_existing_field][field_name]'] = 'field_uf_text_text';
  238. $edit['fields[_add_existing_field][widget_type]'] = 'text_textfield';
  239. $this->drupalPost('admin/structure/types/manage/uf_text2/fields', $edit, t('Save'));
  240. $this->assertText('These settings apply only to the Unique Text 2 Text field when used in the Unique Text 2 type.', 'Field added to content type.');
  241. $edit = array();
  242. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  243. $edit['unique_field_fields[field_uf_text_text]'] = 'field_uf_text_text';
  244. $this->drupalPost('admin/structure/types/manage/uf_text2', $edit, t('Save content type'));
  245. $this->assertText('The content type Unique Text 2 has been updated.', 'Content type updated.');
  246. // Attempt to create 2 nodes with the same text in different content types
  247. $text = $this->randomName(48);
  248. $edit = array();
  249. $edit['title'] = $this->randomName(24);
  250. $edit['body[und][0][value]'] = $this->randomName(48);
  251. $edit['field_uf_text_text[und][0][value]'] = $text;
  252. $this->drupalPost('node/add/uf-text', $edit, t('Save'));
  253. $this->assertText($edit['body[und][0][value]'], 'Unique Text (uf_text) node has been created');
  254. $edit = array();
  255. $edit['title'] = $this->randomName(24);
  256. $edit['body[und][0][value]'] = $this->randomName(48);
  257. $edit['field_uf_text_text[und][0][value]'] = $text;
  258. $this->drupalPost('node/add/uf-text2', $edit, t('Save'));
  259. $this->assertText('The Unique Text 2 Text field requires a unique value, and the specified value is already used', 'Unique Text 2 (uf_text2) node with duplicate content could not be created');
  260. // Check for false negative: Attempt to create a node with a unique text
  261. $edit = array();
  262. $edit['title'] = $this->randomName(24);
  263. $edit['body[und][0][value]'] = $this->randomName(48);
  264. $edit['field_uf_text_text[und][0][value]'] = $this->randomName(48);
  265. $this->drupalPost('node/add/uf-text2', $edit, t('Save'));
  266. $this->assertText($edit['body[und][0][value]'], 'Unique Text 2 (uf_text2) node has been created');
  267. }
  268. /**
  269. * Test the unique requirement on a text field in the single scope.
  270. */
  271. public function testUniqueSingleText() {
  272. // Create a content type with two text fields that are set to be unique
  273. $edit = array();
  274. $edit['name'] = 'Unique Text Single';
  275. $edit['type'] = 'uf_text_single';
  276. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  277. $this->assertText('The content type Unique Text Single has been added.', 'Content type added.');
  278. $edit = array();
  279. $edit['fields[_add_new_field][label]'] = 'Unique Text Single Text 1';
  280. $edit['fields[_add_new_field][field_name]'] = 'uf_text_single_text_1';
  281. $edit['fields[_add_new_field][type]'] = 'text';
  282. $edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
  283. $this->drupalPost('admin/structure/types/manage/uf_text_single/fields', $edit, t('Save'));
  284. $this->assertText('These settings apply to the Unique Text Single Text 1 field everywhere it is used.', 'Field added to content type.');
  285. $edit = array();
  286. $edit['fields[_add_new_field][label]'] = 'Unique Text Single Text 2';
  287. $edit['fields[_add_new_field][field_name]'] = 'uf_text_single_text_2';
  288. $edit['fields[_add_new_field][type]'] = 'text';
  289. $edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
  290. $this->drupalPost('admin/structure/types/manage/uf_text_single/fields', $edit, t('Save'));
  291. $this->assertText('These settings apply to the Unique Text Single Text 2 field everywhere it is used.', 'Field added to content type.');
  292. $edit = array();
  293. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_NODE;
  294. $edit['unique_field_fields[field_uf_text_single_text_1]'] = 'field_uf_text_single_text_1';
  295. $edit['unique_field_fields[field_uf_text_single_text_2]'] = 'field_uf_text_single_text_2';
  296. $this->drupalPost('admin/structure/types/manage/uf_text_single', $edit, t('Save content type'));
  297. $this->assertText('The content type Unique Text Single has been updated.', 'Content type updated.');
  298. // Attempt to create a node with the same text in both fields
  299. $text = $this->randomName(48);
  300. $edit = array();
  301. $edit['title'] = $this->randomName(24);
  302. $edit['body[und][0][value]'] = $this->randomName(48);
  303. $edit['field_uf_text_single_text_1[und][0][value]'] = $text;
  304. $edit['field_uf_text_single_text_2[und][0][value]'] = $text;
  305. $this->drupalPost('node/add/uf-text-single', $edit, t('Save'));
  306. $this->assertText('The Unique Text Single Text 2 fields must have unique values. The Unique Text Single Text 2 field has a value that is already used.', 'Unique Text Single (uf_text_single) node with duplicate content could not be created');
  307. // Check for false negative: Attempt to create a node with unique text
  308. $edit = array();
  309. $edit['title'] = $this->randomName(24);
  310. $edit['body[und][0][value]'] = $this->randomName(48);
  311. $edit['field_uf_text_single_text_1[und][0][value]'] = $this->randomName(48);
  312. $edit['field_uf_text_single_text_2[und][0][value]'] = $this->randomName(48);
  313. $this->drupalPost('node/add/uf-text-single', $edit, t('Save'));
  314. $this->assertText($edit['body[und][0][value]'], 'Unique Text Single (uf_text_single) node has been created');
  315. }
  316. /**
  317. * Test the unique requirement on an integer field in the content type scope.
  318. */
  319. public function testUniqueCtypeInteger() {
  320. // Create a content type with an integer field that is set to be unique
  321. $edit = array();
  322. $edit['name'] = 'Unique Integer';
  323. $edit['type'] = 'uf_int';
  324. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  325. $this->assertText('The content type Unique Integer has been added.', 'Content type added.');
  326. $edit = array();
  327. $edit['fields[_add_new_field][label]'] = 'Unique Integer Integer';
  328. $edit['fields[_add_new_field][field_name]'] = 'uf_int_int';
  329. $edit['fields[_add_new_field][type]'] = 'number_integer';
  330. $edit['fields[_add_new_field][widget_type]'] = 'number';
  331. $this->drupalPost('admin/structure/types/manage/uf_int/fields', $edit, t('Save'));
  332. $this->assertText('These settings apply to the Unique Integer Integer field everywhere it is used.', 'Field added to content type.');
  333. $edit = array();
  334. $edit['unique_field_fields[field_uf_int_int]'] = 'field_uf_int_int';
  335. $this->drupalPost('admin/structure/types/manage/uf_int', $edit, t('Save content type'));
  336. $this->assertText('The content type Unique Integer has been updated.', 'Content type updated.');
  337. // Attempt to create 2 nodes with the same integer value
  338. $num = mt_rand();
  339. $edit = array();
  340. $edit['title'] = $this->randomName(24);
  341. $edit['body[und][0][value]'] = $this->randomName(48);
  342. $edit['field_uf_int_int[und][0][value]'] = $num;
  343. $this->drupalPost('node/add/uf-int', $edit, t('Save'));
  344. $this->assertText($edit['body[und][0][value]'], 'Unique Integer (uf_int) node has been created');
  345. $edit['body[und][0][value]'] = $this->randomName(48);
  346. $this->drupalPost('node/add/uf-int', $edit, t('Save'));
  347. $this->assertText('The Unique Integer Integer field requires a unique value, and the specified value is already used', 'Unique Integer (uf_int) node with duplicate content could not be created');
  348. // Check for false negative: Attempt to create a node with a unique integer
  349. $edit = array();
  350. $edit['title'] = $this->randomName(24);
  351. $edit['body[und][0][value]'] = $this->randomName(48);
  352. $edit['field_uf_int_int[und][0][value]'] = $num - 1;
  353. $this->drupalPost('node/add/uf-int', $edit, t('Save'));
  354. $this->assertText($edit['body[und][0][value]'], 'Unique Integer (uf_int) node has been created');
  355. }
  356. /**
  357. * Test the unique requirement on an integer field in the all scope.
  358. */
  359. public function testUniqueAllInteger() {
  360. // Create a content type with an integer field that is set to be unique
  361. $edit = array();
  362. $edit['name'] = 'Unique Integer';
  363. $edit['type'] = 'uf_int';
  364. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  365. $this->assertText('The content type Unique Integer has been added.', 'Content type added.');
  366. $edit = array();
  367. $edit['fields[_add_new_field][label]'] = 'Unique Integer Integer';
  368. $edit['fields[_add_new_field][field_name]'] = 'uf_int_int';
  369. $edit['fields[_add_new_field][type]'] = 'number_integer';
  370. $edit['fields[_add_new_field][widget_type]'] = 'number';
  371. $this->drupalPost('admin/structure/types/manage/uf_int/fields', $edit, t('Save'));
  372. $this->assertText('These settings apply to the Unique Integer Integer field everywhere it is used.', 'Field added to content type.');
  373. $edit = array();
  374. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  375. $edit['unique_field_fields[field_uf_int_int]'] = 'field_uf_int_int';
  376. $this->drupalPost('admin/structure/types/manage/uf_int', $edit, t('Save content type'));
  377. $this->assertText('The content type Unique Integer has been updated.', 'Content type updated.');
  378. // Create another content type with the same integer field and set it to unique
  379. $edit = array();
  380. $edit['name'] = 'Unique Integer 2';
  381. $edit['type'] = 'uf_int2';
  382. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  383. $this->assertText('The content type Unique Integer 2 has been added.', 'Content type added.');
  384. $edit = array();
  385. $edit['fields[_add_existing_field][label]'] = 'Unique Integer 2 Integer';
  386. $edit['fields[_add_existing_field][field_name]'] = 'field_uf_int_int';
  387. $edit['fields[_add_existing_field][widget_type]'] = 'number';
  388. $this->drupalPost('admin/structure/types/manage/uf_int2/fields', $edit, t('Save'));
  389. $this->assertText('These settings apply only to the Unique Integer 2 Integer field when used in the Unique Integer 2 type.', 'Field added to content type.');
  390. $edit = array();
  391. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  392. $edit['unique_field_fields[field_uf_int_int]'] = 'field_uf_int_int';
  393. $this->drupalPost('admin/structure/types/manage/uf_int2', $edit, t('Save content type'));
  394. $this->assertText('The content type Unique Integer 2 has been updated.', 'Content type updated.');
  395. // Attempt to create 2 nodes with the same integer in different content types
  396. $num = mt_rand();
  397. $edit = array();
  398. $edit['title'] = $this->randomName(24);
  399. $edit['body[und][0][value]'] = $this->randomName(48);
  400. $edit['field_uf_int_int[und][0][value]'] = $num;
  401. $this->drupalPost('node/add/uf-int', $edit, t('Save'));
  402. $this->assertText($edit['body[und][0][value]'], 'Unique Integer (uf_int) node has been created');
  403. $edit = array();
  404. $edit['title'] = $this->randomName(24);
  405. $edit['body[und][0][value]'] = $this->randomName(48);
  406. $edit['field_uf_int_int[und][0][value]'] = $num;
  407. $this->drupalPost('node/add/uf-int2', $edit, t('Save'));
  408. $this->assertText('The Unique Integer 2 Integer field requires a unique value, and the specified value is already used', 'Unique Integer 2 (uf_int2) node with duplicate content could not be created');
  409. // Check for false negative: Attempt to create a node with a unique text
  410. $edit = array();
  411. $edit['title'] = $this->randomName(24);
  412. $edit['body[und][0][value]'] = $this->randomName(48);
  413. $edit['field_uf_int_int[und][0][value]'] = $num - 1;
  414. $this->drupalPost('node/add/uf-int2', $edit, t('Save'));
  415. $this->assertText($edit['body[und][0][value]'], 'Unique Integer 2 (uf_int2) node has been created');
  416. }
  417. /**
  418. * Test the unique requirement on an integer field in the single scope.
  419. */
  420. public function testUniqueSingleInteger() {
  421. // Create a content type with two integer fields that are set to be unique
  422. $edit = array();
  423. $edit['name'] = 'Unique Integer Single';
  424. $edit['type'] = 'uf_int_single';
  425. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  426. $this->assertText('The content type Unique Integer Single has been added.', 'Content type added.');
  427. $edit = array();
  428. $edit['fields[_add_new_field][label]'] = 'Unique Integer Single Integer 1';
  429. $edit['fields[_add_new_field][field_name]'] = 'uf_int_single_int_1';
  430. $edit['fields[_add_new_field][type]'] = 'number_integer';
  431. $edit['fields[_add_new_field][widget_type]'] = 'number';
  432. $this->drupalPost('admin/structure/types/manage/uf_int_single/fields', $edit, t('Save'));
  433. $this->assertText('These settings apply to the Unique Integer Single Integer 1 field everywhere it is used.', 'Field added to content type.');
  434. $edit = array();
  435. $edit['fields[_add_new_field][label]'] = 'Unique Integer Single Integer 2';
  436. $edit['fields[_add_new_field][field_name]'] = 'uf_int_single_int_2';
  437. $edit['fields[_add_new_field][type]'] = 'number_integer';
  438. $edit['fields[_add_new_field][widget_type]'] = 'number';
  439. $this->drupalPost('admin/structure/types/manage/uf_int_single/fields', $edit, t('Save'));
  440. $this->assertText('These settings apply to the Unique Integer Single Integer 2 field everywhere it is used.', 'Field added to content type.');
  441. $edit = array();
  442. $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_NODE;
  443. $edit['unique_field_fields[field_uf_int_single_int_1]'] = 'field_uf_int_single_int_1';
  444. $edit['unique_field_fields[field_uf_int_single_int_2]'] = 'field_uf_int_single_int_2';
  445. $this->drupalPost('admin/structure/types/manage/uf_int_single', $edit, t('Save content type'));
  446. $this->assertText('The content type Unique Integer Single has been updated.', 'Content type updated.');
  447. // Attempt to create a node with the same integer in both fields
  448. $num = mt_rand();
  449. $edit = array();
  450. $edit['title'] = $this->randomName(24);
  451. $edit['body[und][0][value]'] = $this->randomName(48);
  452. $edit['field_uf_int_single_int_1[und][0][value]'] = $num;
  453. $edit['field_uf_int_single_int_2[und][0][value]'] = $num;
  454. $this->drupalPost('node/add/uf-int-single', $edit, t('Save'));
  455. $this->assertText('The Unique Integer Single Integer 2 fields must have unique values. The Unique Integer Single Integer 2 field has a value that is already used.', 'Unique Integer Single (uf_int_single) node with duplicate content could not be created');
  456. // Check for false negative: Attempt to create a node with unique numbers
  457. $edit = array();
  458. $edit['title'] = $this->randomName(24);
  459. $edit['body[und][0][value]'] = $this->randomName(48);
  460. $edit['field_uf_int_single_int_1[und][0][value]'] = $num;
  461. $edit['field_uf_int_single_int_2[und][0][value]'] = $num - 1;
  462. $this->drupalPost('node/add/uf-int-single', $edit, t('Save'));
  463. $this->assertText($edit['body[und][0][value]'], 'Unique Integer Single (uf_int_single) node has been created');
  464. }
  465. }