You are here

function ColodFieldPlainTextTest::testColorFieldDefault in Color Field 7

Same name and namespace in other branches
  1. 7.2 color_field.test \ColodFieldPlainTextTest::testColorFieldDefault()

Test basic functionality of the color field.

  • Creates a content type.
  • Adds a single-valued to it.
  • Adds a multivalued 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

./color_field.test, line 36
Tests for Color Field.

Class

ColodFieldPlainTextTest
Defines a class for testing color field module (Plain Text).

Code

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

  // Create and login user.
  $account = $this
    ->drupalCreateUser(array(
    'administer content types',
  ));
  $this
    ->drupalLogin($account);
  $this
    ->drupalGet('admin/structure/types');

  // Create the content type.
  $this
    ->clickLink(t('Add content type'));
  $edit = array(
    'name' => $content_type_friendly,
    'type' => $content_type_machine,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save and add fields'));
  $this
    ->assertText(t('The content type @name has been added.', array(
    '@name' => $content_type_friendly,
  )));
  $single_field_name_friendly = $this
    ->randomName(20);
  $single_field_name_machine = strtolower($this
    ->randomName(10));
  $multivalue_field_name_friendly = $this
    ->randomName(20);
  $multivalue_field_name_machine = strtolower($this
    ->randomName(10));

  // Description of fields to be created;
  $fields[$single_field_name_machine] = array(
    'widget_type' => 'color_field_default_widget',
    'cardinality' => '1',
    'label' => $single_field_name_friendly,
  );
  $fields[$multivalue_field_name_machine] = array(
    'widget_type' => 'color_field_default_widget',
    'cardinality' => -1,
    'label' => $multivalue_field_name_friendly,
  );
  foreach ($fields as $fieldname => $details) {
    $this
      ->create_field($fieldname, $details);
  }

  // Somehow clicking "save" isn't enough, and we have to do a
  // node_types_rebuild().
  node_types_rebuild();
  menu_rebuild();
  $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(
    ':type' => $content_type_machine,
  ))
    ->fetchField();
  $this
    ->assertTrue($type_exists, 'The new content type has been created in the database.');
  $permission = 'create ' . $content_type_machine . ' content';
  $permission_edit = 'edit ' . $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
    ->drupalCreateUser(array(
    $permission,
  ));
  $this
    ->drupalLogin($account);
  $this
    ->drupalGet('node/add/' . $content_type_machine);

  // Add a node.
  $edit = array(
    'title' => $title,
    'field_' . $single_field_name_machine . '[und][0][rgb]' => '#000001',
    'field_' . $multivalue_field_name_machine . '[und][0][rgb]' => '#000002',
  );

  // We want to add a 2nd item to the multivalue field, so hit "add another".
  $this
    ->drupalPost(NULL, $edit, t('Add another item'));
  $edit = array(
    'field_' . $multivalue_field_name_machine . '[und][1][rgb]' => '#000003',
  );

  // Now we can fill in the second item in the multivalue field and save.
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->assertText(t('@content_type_friendly @title has been created', array(
    '@content_type_friendly' => $content_type_friendly,
    '@title' => $title,
  )));
  $output_strings = $this
    ->xpath("//div[contains(@class,'field-type-color-field-rgb')]/div/div/text()");
  $this
    ->assertEqual((string) $output_strings[0], "#000001");
  $this
    ->assertEqual((string) $output_strings[1], "#000002");
  $this
    ->assertEqual((string) $output_strings[2], "#000003");
}