You are here

timefield.test in Timefield 7

Tests for timefield.

File

timefield.test
View source
<?php

/**
 * @file
 * Tests for timefield.
 */
class TimeFieldTest extends DrupalWebTestCase {

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'Timefield',
      'description' => 'Create a content type with timefield fields, create a node, check for correct values.',
      'group' => 'Fields',
    );
  }

  /**
   * Overrides setUp()
   */
  function setUp() {

    // Enable the timefield module.
    parent::setUp(array(
      'field_ui',
      'timefield',
    ));
  }

  /**
   * Test basic functionality of the timefield.
   *
   * - Creates a content type.
   * - Adds a single-valued timefield to it.
   * - Adds a multivalued timefield to it.
   * - Creates a node of the new type.
   * - Populates the single-valued field.
   * - Populates the multivalued field with two items.
   * - Tests the result.
   */
  function testTimeFieldBasic() {
    $content_type_friendly = $this
      ->randomName(20);
    $content_type_machine = drupal_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'));
    $single_field_name_friendly = $this
      ->randomName(20);
    $single_field_name_machine = drupal_strtolower($this
      ->randomName(10));
    $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,
    )));

    // 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.');

    // Now add a singleton field.
    $edit = array(
      'fields[_add_new_field][label]' => $single_field_name_friendly,
      'fields[_add_new_field][field_name]' => $single_field_name_machine,
      'fields[_add_new_field][type]' => 'timefield',
      'fields[_add_new_field][widget_type]' => 'timefield_standard',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $edit = array(
      'field[settings][totime]' => (string) 'required',
    );
    $this
      ->drupalPost(NULL, array(), t('Save field settings'));
    $this
      ->drupalPost(NULL, $edit, t('Save settings'));
    $this
      ->assertText(t('Saved @name configuration.', array(
      '@name' => $single_field_name_friendly,
    )));

    // Now we're back on the field-add page.
    // Now add a multivalued field.
    $multivalue_field_name_friendly = $this
      ->randomName(20);
    $multivalue_field_name_machine = drupal_strtolower($this
      ->randomName(10));
    $edit = array(
      'fields[_add_new_field][label]' => $multivalue_field_name_friendly,
      'fields[_add_new_field][field_name]' => $multivalue_field_name_machine,
      'fields[_add_new_field][type]' => 'timefield',
      'fields[_add_new_field][widget_type]' => 'timefield_standard',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $edit = array(
      'field[settings][totime]' => (string) 'required',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save field settings'));
    $edit = array(
      'field[cardinality]' => (string) -1,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save settings'));
    $this
      ->assertText(t('Saved @name configuration.', array(
      '@name' => $multivalue_field_name_friendly,
    )));

    // Now we're back on the field-add page.
    // Now add a duration field.
    $duration_field_name_friendly = $this
      ->randomName(20);
    $duration_field_name_machine = drupal_strtolower($this
      ->randomName(10));
    $edit = array(
      'fields[_add_new_field][label]' => $duration_field_name_friendly,
      'fields[_add_new_field][field_name]' => $duration_field_name_machine,
      'fields[_add_new_field][type]' => 'timefield',
      'fields[_add_new_field][widget_type]' => 'timefield_standard',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $edit = array(
      'field[settings][totime]' => (string) 'required',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save field settings'));
    $this
      ->drupalPost(NULL, $edit, t('Save settings'));
    $this
      ->assertText(t('Saved @name configuration.', array(
      '@name' => $duration_field_name_friendly,
    )));

    // Make the display settings for duration field
    $this
      ->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
    $edit = array(
      'fields[field_' . $duration_field_name_machine . '][type]' => 'timefield_duration',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $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
      ->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][value]' => '7:00 am',
      'field_' . $single_field_name_machine . '[und][0][value2]' => '8:00 am',
      'field_' . $multivalue_field_name_machine . '[und][0][value]' => '2:00 pm',
      'field_' . $multivalue_field_name_machine . '[und][0][value2]' => '3:00 pm',
    );

    // We want to add a 2nd item in the multivalue field, so hit "add another".
    $this
      ->drupalPost(NULL, $edit, t('Add another item'));
    $edit = array(
      'field_' . $multivalue_field_name_machine . '[und][1][value]' => '8:00 pm',
      'field_' . $multivalue_field_name_machine . '[und][1][value2]' => '9:00 pm',
      'field_' . $duration_field_name_machine . '[und][0][value]' => '12:00 am',
      'field_' . $duration_field_name_machine . '[und][0][value2]' => '2:00 pm',
    );

    // 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,
    )));
    $this
      ->assertText(t('@time1', array(
      '@time1' => "7:00am - 8:00am",
    )));
    $this
      ->assertText(t('@time2', array(
      '@time2' => "2:00pm - 3:00pm",
    )));
    $this
      ->assertText(t('@time3', array(
      '@time3' => "8:00pm - 9:00pm",
    )));
    $this
      ->assertText(t('@duration', array(
      '@duration' => "840",
    )));
  }

}

/**
 * Timefield Unit tests
 *
 * @see DrupalUnitTestCase
 */
class TimeFieldUnitTest extends DrupalUnitTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Timefield unit tests',
      'description' => 'Test that utility functions work properly.',
      'group' => 'Fields',
    );
  }
  function setUp() {
    drupal_load('module', 'timefield');
    parent::setUp();
  }

  /**
   *  Check that timefield_field_is_empty work correctly
   *
   */
  public function testSimpletestUnitTestExampleFunction() {
    $item = array();
    $result = timefield_field_is_empty($item, array());
    $message = t('Value index not set should return TRUE.');
    $this
      ->assertTrue($result, $message);
    $item['value'] = NULL;
    $result = timefield_field_is_empty($item, array());
    $message = t('A NULL value should return TRUE.');
    $this
      ->assertTrue($result, $message);
    $item['value'] = '';
    $result = timefield_field_is_empty($item, array());
    $message = t('An empty string should return TRUE.');
    $this
      ->assertTrue($result, $message);
    $item['value'] = 50;
    $result = timefield_field_is_empty($item, array());
    $message = t('A positive integer should return FALSE.');
    $this
      ->assertFalse($result, $message);
    $item['value'] = '0';
    $result = timefield_field_is_empty($item, array());
    $message = t('A string with a zero should return FALSE.');
    $this
      ->assertFalse($result, $message);
    $item['value'] = 0;
    $result = timefield_field_is_empty($item, array());
    $message = t('A zero should return FALSE.');
    $this
      ->assertFalse($result, $message);
  }

}

Classes

Namesort descending Description
TimeFieldTest @file Tests for timefield.
TimeFieldUnitTest Timefield Unit tests