field_example.test

  1. examples
    1. 7 field_example/field_example.test
    2. 8 field_example/field_example.test

Tests for Field Example.

Classes

NameDescription
FieldExampleTest

File

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