You are here

RequiredByRoleEmailFieldWebTestCase.test in Required by role 7

Tests for the Email field.

File

tests/required_by_role_test/RequiredByRoleEmailFieldWebTestCase.test
View source
<?php

/**
 * @file
 * Tests for the Email field.
 */
class RequiredByRoleEmailFieldWebTestCase extends RequiredByRoleBasic {

  /**
   * Label of the test field.
   * @var string
   */
  protected $field_label = 'Email';

  /**
   * Getinfo method.
   */
  public static function getInfo() {
    return array(
      'name' => 'Required by role: Email',
      'description' => 'Tests for email field.',
      'group' => 'Required by role',
    );
  }

  /**
   * Setup method.
   */
  protected function setUp() {
    parent::setUp('field_ui', 'required_by_role', 'email');
    $rid = (int) $this->required_fields_rid;
    $settings = array(
      "instance[settings][required_by_role][{$rid}]" => $rid,
    );
    $this
      ->drupalLogin($this
      ->getRequiredUser());
    $this
      ->createEmailField(NULL, $settings);
  }

  /**
   * Test for both required and non-required roles.
   */
  public function testRequiredByRole() {
    $edit = array();
    $edit['title'] = $this
      ->randomName(8);
    $edit['body[und][0][value]'] = $this
      ->randomName(16);
    $this
      ->drupalPost('node/add/' . $this->entity_bundle, $edit, t('Save'));
    $this
      ->assertText($this->field_label . ' field is required.');
    $this
      ->drupalLogout();
    $this
      ->drupalLogin($this
      ->getNonRequiredUser());
    $this
      ->drupalPost('node/add/' . $this->entity_bundle, $edit, t('Save'));
    $this
      ->assertText($edit['title'], "Node has been created");
  }

  /**
   * Helper function to create an email field.
   */
  protected function createEmailField($bundle = NULL, $settings = array()) {
    $node_type = $bundle ? $bundle : $this->entity_bundle;
    $name = 'email_test_' . strtolower($this
      ->randomName(4));
    $label = $this->field_label;
    $edit = array(
      'fields[_add_new_field][label]' => $label,
      'fields[_add_new_field][field_name]' => $name,
      'fields[_add_new_field][type]' => 'email',
      'fields[_add_new_field][widget_type]' => 'email_textfield',
    );
    $field_name = 'field_' . $name;
    $this
      ->drupalPost('admin/structure/types/manage/' . $node_type . '/fields', $edit, t('Save'));
    $this
      ->drupalPost(NULL, array(), t('Save field settings'));
    $this
      ->drupalPost(NULL, $settings, t('Save settings'));

    // Is field created?
    $this
      ->assertRaw(t('Saved %label configuration', array(
      '%label' => $label,
    )), 'Field added');
    node_types_rebuild();
    menu_rebuild();
    return $field_name;
  }

}

Classes

Namesort descending Description
RequiredByRoleEmailFieldWebTestCase @file Tests for the Email field.