1 field_example.test public FieldExampleTest::testExampleFieldBasic()

Test basic functionality of the example field.

  • Creates a content type.
  • Adds a single-valued field_example_rgb to it.
  • Adds a multivalued field_example_rgb to it.
  • Creates a node of the new type.
  • Populates the single-valued field.
  • Populates the multivalued field with two items.
  • Tests the result.

File

modules/examples/field_example/tests/field_example.test, line 33
Tests for Field Example.

Class

FieldExampleTest
Functional tests for the Field Example module.

Code

public function testExampleFieldBasic() {
  $content_type_machine = strtolower($this->randomName(10));
  $title = $this->randomName(20);

  // Create and login user.
  $account = $this->backdropCreateUser(array('administer content types', 'administer fields'));
  $this->backdropLogin($account);

  $this->backdropGet('admin/structure/types');

  // Create the content type.
  $this->clickLink(t('Add content type'));

  $edit = array(
    'name' => $content_type_machine,
    'type' => $content_type_machine,
  );
  $this->backdropPost(NULL, $edit, t('Save and add fields'));
  $this->assertText(t('The content type @name has been added.', array('@name' => $content_type_machine)));

  $single_text_field = strtolower($this->randomName(10));
  $single_colorpicker_field = strtolower($this->randomName(10));
  $single_3text_field = strtolower($this->randomName(10));
  $multivalue_3text_field = strtolower($this->randomName(10));

  // Description of fields to be created;
  $fields[$single_text_field] = array(
    'widget' => 'field_example_text',
    'cardinality' => '1',
  );
  $fields[$single_colorpicker_field] = array(
    'widget' => 'field_example_colorpicker',
    'cardinality' => 1,
  );
  $fields[$single_3text_field] = array(
    'widget' => 'field_example_3text',
    'cardinality' => 1,
  );
  $fields[$multivalue_3text_field] = array(
    'widget' => 'field_example_3text',
    'cardinality' => -1,
  );

  foreach ($fields as $fieldname => $details) {
    $this->createField($fieldname, $details['widget'], $details['cardinality']);
  }

  menu_rebuild();
  //$type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
  $type_exists = config_get('node.type.' . $content_type_machine);
  $this->assertTrue($type_exists, 'The new content type has been created in the database.');

  $permission = 'create ' . $content_type_machine . ' content';
  // Reset the permissions cache.
  $this->checkPermissions(array($permission), TRUE);

  // Now that we have a new content type, create a user that has privileges
  // on the content type.
  $account = $this->backdropCreateUser(array($permission));
  $this->backdropLogin($account);

  $this->backdropGet('node/add/' . $content_type_machine);

  // Add a node.
  $edit = array(
    'title' => $title,
    'field_' . $single_text_field . '[und][0][rgb]' => '#000001',
    'field_' . $single_colorpicker_field . '[und][0][rgb]' => '#000002',

    'field_' . $single_3text_field . '[und][0][rgb][r]' => '00',
    'field_' . $single_3text_field . '[und][0][rgb][g]' => '00',
    'field_' . $single_3text_field . '[und][0][rgb][b]' => '03',

    'field_' . $multivalue_3text_field . '[und][0][rgb][r]' => '00',
    'field_' . $multivalue_3text_field . '[und][0][rgb][g]' => '00',
    'field_' . $multivalue_3text_field . '[und][0][rgb][b]' => '04',

  );
  // We want to add a 2nd item to the multi-value field, so hit "add another".
  $this->backdropPost(NULL, $edit, t('Add another item'));

  $edit = array(
    'field_' . $multivalue_3text_field . '[und][1][rgb][r]' => '00',
    'field_' . $multivalue_3text_field . '[und][1][rgb][g]' => '00',
    'field_' . $multivalue_3text_field . '[und][1][rgb][b]' => '05',
  );
  // Now we can fill in the second item in the multi-value field and save.
  $this->backdropPost(NULL, $edit, t('Save'));
  $this->assertText(t('@content_type_machine @title has been created', array('@content_type_machine' => $content_type_machine, '@title' => $title)));

  $output_strings = $this->xpath("//div[contains(@class,'field-type-field-example-rgb')]/div/div/p/text()");

  $this->assertEqual((string) $output_strings[0], "The color code in this field is #000001");
  $this->assertEqual((string) $output_strings[1], "The color code in this field is #000002");
  $this->assertEqual((string) $output_strings[2], "The color code in this field is #000003");
  $this->assertEqual((string) $output_strings[3], "The color code in this field is #000004");
  $this->assertEqual((string) $output_strings[4], "The color code in this field is #000005");
}