You are here

RequiredByRoleDateFieldWebTestCase.test in Required by role 7

Tests for the Email field.

File

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

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

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

  /**
   * Setup method.
   */
  protected function setUp() {

    // Load the date_api module.
    parent::setUp('field', 'field_ui', 'date_api', 'date', 'date_popup', 'date_tools', 'required_by_role', 'required_by_role_test');
    variable_set('date_popup_timepicker', 'none');
    module_load_include('inc', 'node', 'content_types');
    module_load_include('inc', 'date', 'date_admin');
  }

  /**
   * Grouping testing method.
   */
  public function testDoTests() {
    $this
      ->DotestDatePopupElementForm();
    $this
      ->DotestRequiredRole();
    $this
      ->DotestIsNonRequiredRole();
  }

  /**
   * Testing date_popup element in a non field form.
   */
  public function DotestDatePopupElementForm() {
    $this
      ->drupalGet('form-date-element-test');
    $this
      ->assertNoText('Notice: Undefined index: #instance in required_by_role_', "No undefined index error found.");
  }

  /**
   * Test for required roles.
   *
   * @todo Test with blank default value
   * @todo Test with now default value
   * @todo Test with todate null value
   * @todo Test with todate optional value
   * @todo Test with todate required value
   */
  public function DotestRequiredRole() {
    $this
      ->drupalLogin($this
      ->getRequiredUser());
    $edit = array();
    $edit['title'] = $this
      ->randomName(8);
    $edit['body[und][0][value]'] = $this
      ->randomName(16);
    $rid = (int) $this->required_fields_rid;
    $required_by_role_settings = array(
      $rid => $rid,
    );

    // Create a date fields with simple values.
    foreach (array(
      'date',
      'datestamp',
      'datetime',
    ) as $field_type) {
      foreach (array(
        'date_select',
        'date_popup',
        'date_text',
      ) as $widget_type) {
        $field_name = "field_test_{$widget_type}";
        $label = 'Test';
        $options = array(
          'label' => $label,
          'widget_type' => $widget_type,
          'field_name' => $field_name,
          'field_type' => $field_type,
          'input_format' => 'm/d/Y - H:i',
          'todate' => NULL,
        );
        $this
          ->createDateField($options, $required_by_role_settings);
        $this
          ->drupalPost('node/add/' . $this->entity_bundle, $edit, t('Save'));
        switch ($widget_type) {
          case 'date_select':
            $this
              ->assertText('Month field is required.');
            $this
              ->assertText('Day field is required.');
            $this
              ->assertText('Year field is required.');
            $this
              ->assertText('Hour field is required.');
            $this
              ->assertText('Minute field is required.');
            break;
          case 'date_text':
          case 'date_popup':
            $this
              ->assertText('A valid date is required for ' . $label . ' Start date.');
            $this
              ->assertText('A valid date is required for ' . $label . ' End date.');
            break;
        }
        $this
          ->deleteDateField($label);
      }
    }
    $this
      ->drupalLogout();
  }

  /**
   * Test for non-required role.
   */
  public function DotestIsNonRequiredRole() {
    $this
      ->drupalLogin($this
      ->getNonRequiredUser());
    $edit = array();
    $edit['title'] = $this
      ->randomName(8);
    $edit['body[und][0][value]'] = $this
      ->randomName(16);
    $rid = (int) $this->required_fields_rid;
    $required_by_role_settings = array(
      $rid => $rid,
    );

    // Create a date fields with simple values.
    foreach (array(
      'date',
      'datestamp',
      'datetime',
    ) as $field_type) {
      foreach (array(
        'date_select',
        'date_popup',
        'date_text',
      ) as $widget_type) {
        $field_name = "field_test_{$widget_type}";
        $label = 'Test';
        $options = array(
          'label' => $label,
          'widget_type' => $widget_type,
          'field_name' => $field_name,
          'field_type' => $field_type,
          'input_format' => 'm/d/Y - H:i',
        );
        $this
          ->createDateField($options, $required_by_role_settings);
        $this
          ->drupalPost('node/add/' . $this->entity_bundle, $edit, t('Save'));
        $this
          ->assertText($edit['title'], "Node has been created");
        $this
          ->deleteDateField($label);
      }
    }
    $this
      ->drupalLogout();
  }

  /**
   * Creates a date field from an array of settings values.
   *
   * All values have defaults, only need to specify values that need to be
   * different.
   */
  protected function createDateField($values = array(), $required_by_role_settings = array()) {
    extract($values);
    $default_granularity = array(
      'year',
      'month',
      'day',
      'hour',
      'minute',
    );
    $field_name = !empty($field_name) ? $field_name : 'field_test';
    $entity_type = !empty($entity_type) ? $entity_type : 'node';
    $bundle = !empty($bundle) ? $bundle : 'article';
    $label = !empty($label) ? $label : 'Test';
    $field_type = !empty($field_type) ? $field_type : 'datetime';
    $repeat = !empty($repeat) ? $repeat : 0;
    $todate = !empty($todate) ? $todate : 'optional';
    $widget_type = !empty($widget_type) ? $widget_type : 'date_select';
    $tz_handling = !empty($tz_handing) ? $tz_handling : 'site';
    $granularity = !empty($granularity) ? $granularity : $default_granularity;
    $year_range = !empty($year_range) ? $year_range : '2010:+1';
    $input_format = !empty($input_format) ? $input_format : date_default_format($widget_type);
    $input_format_custom = !empty($input_format_custom) ? $input_format_custom : '';
    $text_parts = !empty($text_parts) ? $text_parts : array();
    $increment = !empty($increment) ? $increment : 15;
    $default_value = !empty($default_value) ? $default_value : 'blank';
    $default_value2 = !empty($default_value2) ? $default_value2 : 'blank';
    $default_format = !empty($default_format) ? $default_format : 'long';
    $cache_enabled = !empty($cache_enabled);
    $cache_count = !empty($cache_count) ? $cache_count : 4;
    $field = array(
      'field_name' => $field_name,
      'type' => $field_type,
      'cardinality' => !empty($repeat) ? FIELD_CARDINALITY_UNLIMITED : 1,
      'settings' => array(
        'granularity' => $granularity,
        'tz_handling' => $tz_handling,
        'timezone_db' => date_get_timezone_db($tz_handling),
        'repeat' => $repeat,
        'todate' => $todate,
        'cache_enabled' => $cache_enabled,
        'cache_count' => $cache_count,
      ),
    );
    $instance = array(
      'entity_type' => $entity_type,
      'field_name' => $field_name,
      'label' => $label,
      'bundle' => $bundle,
      // Move the date right below the title.
      'weight' => -4,
      'widget' => array(
        'type' => $widget_type,
        // Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
        'settings' => array(
          'increment' => $increment,
          // The number of years to go back and forward in drop-down year
          // selectors.
          'year_range' => $year_range,
          'input_format' => $input_format,
          'input_format_custom' => $input_format_custom,
          'text_parts' => $text_parts,
          'label_position' => 'above',
          'repeat_collapsed' => 0,
        ),
        'weight' => -4,
      ),
      'settings' => array(
        'default_value' => $default_value,
        'default_value2' => $default_value2,
        'required_by_role' => $required_by_role_settings,
      ),
    );
    $instance['display'] = array(
      'default' => array(
        'label' => 'above',
        'type' => 'date_default',
        'settings' => array(
          'format_type' => $default_format,
          'show_repeat_rule' => 'show',
          'multiple_number' => '',
          'multiple_from' => '',
          'multiple_to' => '',
          'fromto' => 'both',
        ),
        'module' => 'date',
        'weight' => 0,
      ),
      'teaser' => array(
        'label' => 'above',
        'type' => 'date_default',
        'weight' => 0,
        'settings' => array(
          'format_type' => $default_format,
          'show_repeat_rule' => 'show',
          'multiple_number' => '',
          'multiple_from' => '',
          'multiple_to' => '',
          'fromto' => 'both',
        ),
        'module' => 'date',
      ),
    );
    $field = field_create_field($field);
    $instance = field_create_instance($instance);
    field_info_cache_clear(TRUE);
    field_cache_clear(TRUE);

    // Look at how the field got configured.
    // $this->drupalGet("admin/structure/types/manage/$bundle/fields/$field_name");
    // $this->drupalGet("admin/structure/types/manage/$bundle/display");
  }

  /**
   * Helper method to delete a date field.
   */
  protected function deleteDateField($label, $bundle = 'article', $bundle_name = 'Article') {
    $this
      ->drupalGet("admin/structure/types/manage/{$bundle}/fields");
    $this
      ->clickLink('delete');
    $this
      ->drupalPost(NULL, NULL, t('Delete'));
    $this
      ->assertText("The field {$label} has been deleted from the {$bundle_name} content type.", 'Removed date field.');
  }

}

Classes

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