timefield.test 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * @file
  4. * Tests for timefield.
  5. */
  6. class TimeFieldTest extends DrupalWebTestCase {
  7. /**
  8. * Implements getInfo().
  9. */
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Timefield',
  13. 'description' => 'Create a content type with timefield fields, create a node, check for correct values.',
  14. 'group' => 'Fields',
  15. );
  16. }
  17. /**
  18. * Overrides setUp()
  19. */
  20. function setUp() {
  21. // Enable the timefield module.
  22. parent::setUp(array('field_ui', 'timefield'));
  23. }
  24. /**
  25. * Test basic functionality of the timefield.
  26. *
  27. * - Creates a content type.
  28. * - Adds a single-valued timefield to it.
  29. * - Adds a multivalued timefield to it.
  30. * - Creates a node of the new type.
  31. * - Populates the single-valued field.
  32. * - Populates the multivalued field with two items.
  33. * - Tests the result.
  34. */
  35. function testTimeFieldBasic() {
  36. $content_type_friendly = $this->randomName(20);
  37. $content_type_machine = drupal_strtolower($this->randomName(10));
  38. $title = $this->randomName(20);
  39. // Create and login user.
  40. $account = $this->drupalCreateUser(array('administer content types'));
  41. $this->drupalLogin($account);
  42. $this->drupalGet('admin/structure/types');
  43. // Create the content type.
  44. $this->clickLink(t('Add content type'));
  45. $single_field_name_friendly = $this->randomName(20);
  46. $single_field_name_machine = drupal_strtolower($this->randomName(10));
  47. $edit = array(
  48. 'name' => $content_type_friendly,
  49. 'type' => $content_type_machine,
  50. );
  51. $this->drupalPost(NULL, $edit, t('Save and add fields'));
  52. $this->assertText(t('The content type @name has been added.', array('@name' => $content_type_friendly)));
  53. // Somehow clicking "save" isn't enough, and we have to do a
  54. // node_types_rebuild().
  55. node_types_rebuild();
  56. menu_rebuild();
  57. $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
  58. $this->assertTrue($type_exists, 'The new content type has been created in the database.');
  59. // Now add a singleton field.
  60. $edit = array(
  61. 'fields[_add_new_field][label]' => $single_field_name_friendly,
  62. 'fields[_add_new_field][field_name]' => $single_field_name_machine,
  63. 'fields[_add_new_field][type]' => 'timefield',
  64. 'fields[_add_new_field][widget_type]' => 'timefield_standard',
  65. );
  66. $this->drupalPost(NULL, $edit, t('Save'));
  67. $edit = array('field[settings][totime]' => (string) ('required'));
  68. $this->drupalPost(NULL, array(), t('Save field settings'));
  69. $this->drupalPost(NULL, $edit, t('Save settings'));
  70. $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
  71. // Now we're back on the field-add page.
  72. // Now add a multivalued field.
  73. $multivalue_field_name_friendly = $this->randomName(20);
  74. $multivalue_field_name_machine = drupal_strtolower($this->randomName(10));
  75. $edit = array(
  76. 'fields[_add_new_field][label]' => $multivalue_field_name_friendly,
  77. 'fields[_add_new_field][field_name]' => $multivalue_field_name_machine,
  78. 'fields[_add_new_field][type]' => 'timefield',
  79. 'fields[_add_new_field][widget_type]' => 'timefield_standard',
  80. );
  81. $this->drupalPost(NULL, $edit, t('Save'));
  82. $edit = array('field[settings][totime]' => (string) ('required'));
  83. $this->drupalPost(NULL, $edit, t('Save field settings'));
  84. $edit = array(
  85. 'field[cardinality]' => (string) (-1),
  86. );
  87. $this->drupalPost(NULL, $edit, t('Save settings'));
  88. $this->assertText(t('Saved @name configuration.', array('@name' => $multivalue_field_name_friendly)));
  89. // Now we're back on the field-add page.
  90. // Now add a duration field.
  91. $duration_field_name_friendly = $this->randomName(20);
  92. $duration_field_name_machine = drupal_strtolower($this->randomName(10));
  93. $edit = array(
  94. 'fields[_add_new_field][label]' => $duration_field_name_friendly,
  95. 'fields[_add_new_field][field_name]' => $duration_field_name_machine,
  96. 'fields[_add_new_field][type]' => 'timefield',
  97. 'fields[_add_new_field][widget_type]' => 'timefield_standard',
  98. );
  99. $this->drupalPost(NULL, $edit, t('Save'));
  100. $edit = array('field[settings][totime]' => (string) ('required'));
  101. $this->drupalPost(NULL, $edit, t('Save field settings'));
  102. $this->drupalPost(NULL, $edit, t('Save settings'));
  103. $this->assertText(t('Saved @name configuration.', array('@name' => $duration_field_name_friendly)));
  104. // Make the display settings for duration field
  105. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  106. $edit = array(
  107. 'fields[field_' . $duration_field_name_machine . '][type]' => 'timefield_duration',
  108. );
  109. $this->drupalPost(NULL, $edit, t('Save'));
  110. $permission = 'create ' . $content_type_machine . ' content';
  111. // Reset the permissions cache.
  112. $this->checkPermissions(array($permission), TRUE);
  113. // Now that we have a new content type, create a user that has privileges
  114. // on the content type.
  115. $account = $this->drupalCreateUser(array($permission));
  116. $this->drupalLogin($account);
  117. $this->drupalGet('node/add/' . $content_type_machine);
  118. // Add a node.
  119. $edit = array(
  120. 'title' => $title,
  121. 'field_' . $single_field_name_machine . '[und][0][value]' => '7:00 am',
  122. 'field_' . $single_field_name_machine . '[und][0][value2]' => '8:00 am',
  123. 'field_' . $multivalue_field_name_machine . '[und][0][value]' => '2:00 pm',
  124. 'field_' . $multivalue_field_name_machine . '[und][0][value2]' => '3:00 pm',
  125. );
  126. // We want to add a 2nd item in the multivalue field, so hit "add another".
  127. $this->drupalPost(NULL, $edit, t('Add another item'));
  128. $edit = array(
  129. 'field_' . $multivalue_field_name_machine . '[und][1][value]' => '8:00 pm',
  130. 'field_' . $multivalue_field_name_machine . '[und][1][value2]' => '9:00 pm',
  131. 'field_' . $duration_field_name_machine . '[und][0][value]' => '12:00 am',
  132. 'field_' . $duration_field_name_machine . '[und][0][value2]' => '2:00 pm',
  133. );
  134. // Now we can fill in the second item in the multivalue field and save.
  135. $this->drupalPost(NULL, $edit, t('Save'));
  136. $this->assertText(t('@content_type_friendly @title has been created', array('@content_type_friendly' => $content_type_friendly, '@title' => $title)));
  137. $this->assertText(t('@time1', array('@time1' => "7:00am - 8:00am")));
  138. $this->assertText(t('@time2', array('@time2' => "2:00pm - 3:00pm")));
  139. $this->assertText(t('@time3', array('@time3' => "8:00pm - 9:00pm")));
  140. $this->assertText(t('@duration', array('@duration' => "840")));
  141. }
  142. }
  143. /**
  144. * Timefield Unit tests
  145. *
  146. * @see DrupalUnitTestCase
  147. */
  148. class TimeFieldUnitTest extends DrupalUnitTestCase {
  149. public static function getInfo() {
  150. return array(
  151. 'name' => 'Timefield unit tests',
  152. 'description' => 'Test that utility functions work properly.',
  153. 'group' => 'Fields',
  154. );
  155. }
  156. function setUp() {
  157. parent::setUp('timefield');
  158. }
  159. /**
  160. * Check that timefield_field_is_empty work correctly
  161. *
  162. */
  163. public function testSimpletestUnitTestExampleFunction() {
  164. $item = array();
  165. $result = timefield_field_is_empty($item, array());
  166. $message = t('Value index not set should return TRUE.');
  167. $this->assertTrue($result, $message);
  168. $item['value'] = NULL;
  169. $result = timefield_field_is_empty($item, array());
  170. $message = t('A NULL value should return TRUE.');
  171. $this->assertTrue($result, $message);
  172. $item['value'] = '';
  173. $result = timefield_field_is_empty($item, array());
  174. $message = t('An empty string should return TRUE.');
  175. $this->assertTrue($result, $message);
  176. $item['value'] = 50;
  177. $result = timefield_field_is_empty($item, array());
  178. $message = t('A positive integer should return FALSE.');
  179. $this->assertFalse($result, $message);
  180. $item['value'] = '0';
  181. $result = timefield_field_is_empty($item, array());
  182. $message = t('A string with a zero should return FALSE.');
  183. $this->assertFalse($result, $message);
  184. $item['value'] = 0;
  185. $result = timefield_field_is_empty($item, array());
  186. $message = t('A zero should return FALSE.');
  187. $this->assertFalse($result, $message);
  188. }
  189. }