field_example.test 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Field Example.
  5. */
  6. /**
  7. * Functional tests for the Field Example module.
  8. *
  9. * @ingroup field_example
  10. */
  11. class FieldExampleTest extends DrupalWebTestCase {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'Field Example',
  18. 'description' => 'Create a content type with example_field_rgb fields, create a node, check for correct values.',
  19. 'group' => 'Examples',
  20. );
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function setUp() {
  26. // Enable the email_example module.
  27. parent::setUp(array('field_ui', 'field_example'));
  28. }
  29. /**
  30. * Test basic functionality of the example field.
  31. *
  32. * - Creates a content type.
  33. * - Adds a single-valued field_example_rgb to it.
  34. * - Adds a multivalued field_example_rgb to it.
  35. * - Creates a node of the new type.
  36. * - Populates the single-valued field.
  37. * - Populates the multivalued field with two items.
  38. * - Tests the result.
  39. */
  40. public function testExampleFieldBasic() {
  41. $content_type_machine = strtolower($this->randomName(10));
  42. $title = $this->randomName(20);
  43. // Create and login user.
  44. $account = $this->drupalCreateUser(array('administer content types', 'administer fields'));
  45. $this->drupalLogin($account);
  46. $this->drupalGet('admin/structure/types');
  47. // Create the content type.
  48. $this->clickLink(t('Add content type'));
  49. $edit = array(
  50. 'name' => $content_type_machine,
  51. 'type' => $content_type_machine,
  52. );
  53. $this->drupalPost(NULL, $edit, t('Save and add fields'));
  54. $this->assertText(t('The content type @name has been added.', array('@name' => $content_type_machine)));
  55. $single_text_field = strtolower($this->randomName(10));
  56. $single_colorpicker_field = strtolower($this->randomName(10));
  57. $single_3text_field = strtolower($this->randomName(10));
  58. $multivalue_3text_field = strtolower($this->randomName(10));
  59. // Description of fields to be created;
  60. $fields[$single_text_field] = array(
  61. 'widget' => 'field_example_text',
  62. 'cardinality' => '1',
  63. );
  64. $fields[$single_colorpicker_field] = array(
  65. 'widget' => 'field_example_colorpicker',
  66. 'cardinality' => 1,
  67. );
  68. $fields[$single_3text_field] = array(
  69. 'widget' => 'field_example_3text',
  70. 'cardinality' => 1,
  71. );
  72. $fields[$multivalue_3text_field] = array(
  73. 'widget' => 'field_example_3text',
  74. 'cardinality' => -1,
  75. );
  76. foreach ($fields as $fieldname => $details) {
  77. $this->createField($fieldname, $details['widget'], $details['cardinality']);
  78. }
  79. // Somehow clicking "save" isn't enough, and we have to do a
  80. // node_types_rebuild().
  81. node_types_rebuild();
  82. menu_rebuild();
  83. $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
  84. $this->assertTrue($type_exists, 'The new content type has been created in the database.');
  85. $permission = 'create ' . $content_type_machine . ' content';
  86. // Reset the permissions cache.
  87. $this->checkPermissions(array($permission), TRUE);
  88. // Now that we have a new content type, create a user that has privileges
  89. // on the content type.
  90. $account = $this->drupalCreateUser(array($permission));
  91. $this->drupalLogin($account);
  92. $this->drupalGet('node/add/' . $content_type_machine);
  93. // Add a node.
  94. $edit = array(
  95. 'title' => $title,
  96. 'field_' . $single_text_field . '[und][0][rgb]' => '#000001',
  97. 'field_' . $single_colorpicker_field . '[und][0][rgb]' => '#000002',
  98. 'field_' . $single_3text_field . '[und][0][rgb][r]' => '00',
  99. 'field_' . $single_3text_field . '[und][0][rgb][g]' => '00',
  100. 'field_' . $single_3text_field . '[und][0][rgb][b]' => '03',
  101. 'field_' . $multivalue_3text_field . '[und][0][rgb][r]' => '00',
  102. 'field_' . $multivalue_3text_field . '[und][0][rgb][g]' => '00',
  103. 'field_' . $multivalue_3text_field . '[und][0][rgb][b]' => '04',
  104. );
  105. // We want to add a 2nd item to the multivalue field, so hit "add another".
  106. $this->drupalPost(NULL, $edit, t('Add another item'));
  107. $edit = array(
  108. 'field_' . $multivalue_3text_field . '[und][1][rgb][r]' => '00',
  109. 'field_' . $multivalue_3text_field . '[und][1][rgb][g]' => '00',
  110. 'field_' . $multivalue_3text_field . '[und][1][rgb][b]' => '05',
  111. );
  112. // Now we can fill in the second item in the multivalue field and save.
  113. $this->drupalPost(NULL, $edit, t('Save'));
  114. $this->assertText(t('@content_type_machine @title has been created', array('@content_type_machine' => $content_type_machine, '@title' => $title)));
  115. $output_strings = $this->xpath("//div[contains(@class,'field-type-field-example-rgb')]/div/div/p/text()");
  116. $this->assertEqual((string) $output_strings[0], "The color code in this field is #000001");
  117. $this->assertEqual((string) $output_strings[1], "The color code in this field is #000002");
  118. $this->assertEqual((string) $output_strings[2], "The color code in this field is #000003");
  119. $this->assertEqual((string) $output_strings[3], "The color code in this field is #000004");
  120. $this->assertEqual((string) $output_strings[4], "The color code in this field is #000005");
  121. }
  122. /**
  123. * Utility function to create fields on a content type.
  124. *
  125. * @param string $field_name
  126. * Name of the field, like field_something
  127. * @param string $widget_type
  128. * Widget type, like field_example_3text
  129. * @param int $cardinality
  130. * Cardinality
  131. */
  132. protected function createField($field_name, $widget_type, $cardinality) {
  133. // Add a singleton field_example_text field.
  134. $edit = array(
  135. 'fields[_add_new_field][label]' => $field_name,
  136. 'fields[_add_new_field][field_name]' => $field_name,
  137. 'fields[_add_new_field][type]' => 'field_example_rgb',
  138. 'fields[_add_new_field][widget_type]' => $widget_type,
  139. );
  140. $this->drupalPost(NULL, $edit, t('Save'));
  141. // There are no settings for this, so just press the button.
  142. $this->drupalPost(NULL, array(), t('Save field settings'));
  143. $edit = array('field[cardinality]' => (string) $cardinality);
  144. // Using all the default settings, so press the button.
  145. $this->drupalPost(NULL, $edit, t('Save settings'));
  146. debug(
  147. t('Saved settings for field %field_name with widget %widget_type and cardinality %cardinality',
  148. array(
  149. '%field_name' => $field_name,
  150. '%widget_type' => $widget_type,
  151. '%cardinality' => $cardinality,
  152. )
  153. )
  154. );
  155. $this->assertText(t('Saved @name configuration.', array('@name' => $field_name)));
  156. }
  157. }