date.test 13 KB

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