You are here

ffc.test in Field formatter conditions 7

Test file for Field formatter conditions.

File

tests/ffc.test
View source
<?php

/**
 * @file
 * Test file for Field formatter conditions.
 */

/**
 * Group UI tests.
 */
class FFCTestCase extends DrupalWebTestCase {
  public $nodeBodyValue = '';
  public static function getInfo() {
    return array(
      'name' => 'Field formatter conditions',
      'description' => 'Test built-in field formatter conditions.',
      'group' => 'Field formatter conditions',
    );
  }
  function setUp() {
    parent::setUp('ffc', 'ffc_test');

    // Create test user.
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'administer content types',
      'access administration pages',
      'administer nodes',
      'bypass node access',
    ));
    $this
      ->drupalLogin($this->admin_user);

    // Create tags.
    $this->terms = array(
      'Tag 1',
      'Tag 2',
      'Tag 3',
      'Tag 4',
      'Tag 5',
    );
    foreach ($this->terms as $tag) {
      $term = new stdClass();
      $term->name = $tag;
      $term->vid = 1;
      taxonomy_term_save($term);
    }

    // Create test node.
    $settings = array(
      'type' => 'article',
    );
    $tags = array(
      'value' => $this
        ->randomName(32),
      'format' => filter_default_format(),
    );
    $settings['field_tags'][LANGUAGE_NONE] = array(
      0 => array(
        'tid' => 1,
      ),
      1 => array(
        'tid' => 2,
      ),
      2 => array(
        'tid' => 3,
      ),
    );
    $this->node1 = $this
      ->drupalCreateNode($settings);
    $this->node1->name = $this->admin_user->name;

    // Verify everything is visible on node/1
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertText($this->node1->body[$this->node1->language][0]['value']);
    $this
      ->assertText($this->terms[0]);
    $this
      ->assertText($this->terms[1]);
    $this
      ->assertText($this->terms[2]);

    // Store the body value of node/1 for later use
    $this->nodeBodyValue = $this->node1->body[$this->node1->language][0]['value'];
  }

  /**
   * Edit field formatter settings
   */
  function ffcEditFormatterSettings($condition, $configuration, $element_value = 'edit body', $url = 'admin/structure/types/manage/article/display') {
    $this
      ->drupalPost($url, array(), $element_value);
    $this
      ->drupalPost(NULL, $condition, t('Refresh'));
    $this
      ->drupalPost(NULL, $configuration, t('Update'));
    $this
      ->drupalPost(NULL, array(), t('Save'));
  }

  /**
   * Check Field types.
   */
  function ffcCheckFieldTypes($element_value, $available, $url = 'admin/structure/types/manage/article/display') {
    $this
      ->drupalPost($url, array(), $element_value);
    $this
      ->assertRaw('hide_not_empty');
    $this
      ->assertRaw('hide_on_role');
    $this
      ->assertRaw('hide_on_pages');
    if ($available) {
      $this
        ->assertRaw('test_condition');
    }
    else {
      $this
        ->assertNoRaw('test_condition');
    }
  }

  /**
   * Select a layout.
   */
  function ffcSelectLayout($edit = array(), $assert = array(), $url = 'admin/structure/types/manage/article/display', $options = array()) {
    $edit += array(
      'additional_settings[layout]' => 'ds_2col_stacked',
    );
    $this
      ->drupalPost($url, $edit, t('Save'), $options);
    $assert += array(
      'regions' => array(
        'header' => '<td colspan="8">' . t('Header') . '</td>',
        'left' => '<td colspan="8">' . t('Left') . '</td>',
        'right' => '<td colspan="8">' . t('Right') . '</td>',
        'footer' => '<td colspan="8">' . t('Footer') . '</td>',
      ),
    );
    foreach ($assert['regions'] as $region => $raw) {
      $this
        ->assertRaw($region, t('Region !region found', array(
        '!region' => $region,
      )));
    }
  }

  /**
   * Configure a DS layout.
   */
  function ffcConfigureUI($edit, $url = 'admin/structure/types/manage/article/display') {
    $this
      ->drupalPost($url, $edit, t('Save'));
  }

  /**
   * Test Vannilla Core.
   */
  function testVannillaCore() {
    $this
      ->CoreTestHideSourceTargetNotEmpty();
    $this
      ->CoreTestHideSourceTargetIfEmpty();
    $this
      ->CoreTestHideSourceTargetNoString();
    $this
      ->CoreTestHideSourceTargetIfString();
    $this
      ->CoreTestHideSourceUserRoles();
    $this
      ->CoreTestHideSourcePages();
    $this
      ->CoreTestFieldTypesLimit();
  }

  /**
   * Tests with Display Suite.
   */
  function testDisplaySuite() {
    $ds_found = module_enable(array(
      'ds',
    ));
    if ($ds_found) {
      $this
        ->ffcSelectLayout();
      $edit = array(
        'fields[body][region]' => 'left',
        'fields[submitted_by][region]' => 'right',
      );
      $this
        ->ffcConfigureUI($edit);
      $this
        ->DSTestHideSourceTargetNotEmpty();
      $this
        ->DSTestHideSourceTargetIfEmpty();
      $this
        ->DSTestHideSourceUserRoles();
      $this
        ->DSTestHideSourcePages();
    }
    else {
      $this
        ->assertTrue(TRUE, 'No DS tests run.');
    }
  }

  /**
   * Tests whether a condition is limited to field type or not.
   * It uses the test_condition that is defined in the ffc_test.module
   * that only applies to the image field type.
   */
  function CoreTestFieldTypesLimit() {
    $this
      ->ffcCheckFieldTypes('edit body', FALSE);
    $this
      ->ffcCheckFieldTypes('edit field_image', TRUE);
  }

  /**
   * Test Hide source when target is not empty condition on Core.
   */
  function CoreTestHideSourceTargetNotEmpty() {

    // Hide body when tags are not empty.
    $condition = array(
      'fields[body][settings_edit_form][settings][conditions][0][condition]' => 'hide_not_empty',
    );
    $configuration = array(
      'fields[body][settings_edit_form][settings][conditions][0][configuration][target]' => 'field_tags',
    );
    $this
      ->ffcEditFormatterSettings($condition, $configuration);
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertNoText($this->node1->body[$this->node1->language][0]['value']);
    $this
      ->assertText($this->terms[0]);
    $this
      ->assertText($this->terms[1]);
    $this
      ->assertText($this->terms[2]);

    // Empty tags.
    $edit = array(
      'field_tags[und]' => '',
    );
    $this
      ->drupalPost('node/' . $this->node1->nid . '/edit', $edit, t('Save'));
    $this
      ->assertText($this->node1->body[$this->node1->language][0]['value']);
    $this
      ->assertNoText($this->terms[0]);
    $this
      ->assertNoText($this->terms[1]);
    $this
      ->assertNoText($this->terms[2]);
  }

  /**
   * Test Hide source if target is empty condition on Core.
   */
  function CoreTestHideSourceTargetIfEmpty() {

    // Hide body when tags are empty.
    $condition = array(
      'fields[body][settings_edit_form][settings][conditions][0][condition]' => 'hide_if_empty',
    );
    $configuration = array(
      'fields[body][settings_edit_form][settings][conditions][0][configuration][target]' => 'field_tags',
    );
    $this
      ->ffcEditFormatterSettings($condition, $configuration);

    // Populate our node with tags again
    $edit = array(
      'field_tags[und]' => 'Tag 1, Tag 2, Tag 3, Tag 4, Tag 5',
    );
    $this
      ->drupalPost('node/' . $this->node1->nid . '/edit', $edit, t('Save'));
    $this
      ->assertText($this->node1->body[$this->node1->language][0]['value']);
    $this
      ->assertText($this->terms[0]);
    $this
      ->assertText($this->terms[1]);
    $this
      ->assertText($this->terms[2]);

    // Empty tags.
    $edit = array(
      'field_tags[und]' => '',
    );
    $this
      ->drupalPost('node/' . $this->node1->nid . '/edit', $edit, t('Save'));
    $this
      ->assertNoText($this->node1->body[$this->node1->language][0]['value']);
    $this
      ->assertNoText($this->terms[0]);
    $this
      ->assertNoText($this->terms[1]);
    $this
      ->assertNoText($this->terms[2]);
  }

  /**
   * Test Hide source when target does not contain a string condition on Core.
   */
  function CoreTestHideSourceTargetNoString() {

    // Hide body when tags are not empty.
    $condition = array(
      'fields[body][settings_edit_form][settings][conditions][0][condition]' => 'hide_no_string',
    );
    $configuration = array(
      'fields[body][settings_edit_form][settings][conditions][0][configuration][target]' => 'field_tags',
      'fields[body][settings_edit_form][settings][conditions][0][configuration][string]' => 'Tag 1',
    );
    $this
      ->ffcEditFormatterSettings($condition, $configuration);

    // Populate our node with tags again
    $edit = array(
      'field_tags[und]' => 'Tag 1, Tag 2, Tag 3, Tag 4, Tag 5',
    );
    $this
      ->drupalPost('node/' . $this->node1->nid . '/edit', $edit, t('Save'));

    //$this->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertText($this->node1->body[$this->node1->language][0]['value']);
    $this
      ->assertText($this->terms[0]);
    $this
      ->assertText($this->terms[1]);
    $this
      ->assertText($this->terms[2]);

    // Empty tags.
    $edit = array(
      'field_tags[und]' => '',
    );
    $this
      ->drupalPost('node/' . $this->node1->nid . '/edit', $edit, t('Save'));
    $this
      ->assertNoText($this->node1->body[$this->node1->language][0]['value']);
    $this
      ->assertNoText($this->terms[0]);
    $this
      ->assertNoText($this->terms[1]);
    $this
      ->assertNoText($this->terms[2]);
  }

  /**
   * Test Hide source when target contains a string condition on Core.
   */
  function CoreTestHideSourceTargetIfString() {

    // Hide body when tags are not empty.
    $condition = array(
      'fields[body][settings_edit_form][settings][conditions][0][condition]' => 'hide_if_string',
    );
    $configuration = array(
      'fields[body][settings_edit_form][settings][conditions][0][configuration][target]' => 'field_tags',
      'fields[body][settings_edit_form][settings][conditions][0][configuration][string]' => 'Tag 1',
    );
    $this
      ->ffcEditFormatterSettings($condition, $configuration);

    // Populate our node with tags again
    $edit = array(
      'field_tags[und]' => 'Tag 1, Tag 2, Tag 3, Tag 4, Tag 5',
    );
    $this
      ->drupalPost('node/' . $this->node1->nid . '/edit', $edit, t('Save'));

    //$this->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertNoText($this->node1->body[$this->node1->language][0]['value']);
    $this
      ->assertText($this->terms[0]);
    $this
      ->assertText($this->terms[1]);
    $this
      ->assertText($this->terms[2]);

    // Empty tags.
    $edit = array(
      'field_tags[und]' => '',
    );
    $this
      ->drupalPost('node/' . $this->node1->nid . '/edit', $edit, t('Save'));
    $this
      ->assertText($this->node1->body[$this->node1->language][0]['value']);
    $this
      ->assertNoText($this->terms[0]);
    $this
      ->assertNoText($this->terms[1]);
    $this
      ->assertNoText($this->terms[2]);
  }

  /**
   * Test Hide source when current user has certain roles on Core.
   */
  function CoreTestHideSourceUserRoles() {

    // Hide body when user is anonymous.
    $condition = array(
      'fields[body][settings_edit_form][settings][conditions][0][condition]' => 'hide_on_role',
    );
    $configuration = array(
      'fields[body][settings_edit_form][settings][conditions][0][configuration][roles][]' => '1',
    );
    $this
      ->ffcEditFormatterSettings($condition, $configuration);

    // Verify the body is still there for the authenticated user.
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertText($this->node1->body[$this->node1->language][0]['value']);

    // Log out and verify body is not seen.
    $this
      ->drupalLogout();
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertNoText($this->node1->body[$this->node1->language][0]['value']);

    // Login in again.
    $this
      ->drupalLogin($this->admin_user);
  }

  /**
   * Test Hide source on specific pages on Core.
   */
  function CoreTestHideSourcePages() {

    // Hide fields on specific pages.
    $condition = array(
      'fields[body][settings_edit_form][settings][conditions][0][condition]' => 'hide_on_pages',
    );
    $configuration = array(
      'fields[body][settings_edit_form][settings][conditions][0][configuration][pages]' => 'node/1/testing',
      'fields[body][settings_edit_form][settings][conditions][0][configuration][visibility]' => '1',
    );
    $this
      ->ffcEditFormatterSettings($condition, $configuration);

    // Verify the body is on node/1.
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertText($this->node1->body[$this->node1->language][0]['value']);

    // Verify the body is not on node/1/testing.
    $this
      ->drupalGet('node/' . $this->node1->nid . '/testing');
    $this
      ->assertNoText($this->node1->body[$this->node1->language][0]['value']);
  }

  /**
   * Test Hide source when target is not empty condition on DS
   */
  function DSTestHideSourceTargetNotEmpty() {

    // Hide submitted_by when body is not empty.
    $condition = array(
      'fields[submitted_by][settings_edit_form][settings][conditions][0][condition]' => 'hide_not_empty',
    );
    $configuration = array(
      'fields[submitted_by][settings_edit_form][settings][conditions][0][configuration][target]' => 'body',
    );
    $this
      ->ffcEditFormatterSettings($condition, $configuration, 'edit submitted_by');
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $body_value = $this->node1->body[$this->node1->language][0]['value'];
    $this
      ->assertText($body_value);
    $this
      ->assertNoRaw(check_plain($this->node1->name));

    // Empty body.
    $edit = array(
      'body[und][0][value]' => '',
    );
    $this
      ->drupalPost('node/' . $this->node1->nid . '/edit', $edit, t('Save'));
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertNoText($body_value);
    $this
      ->assertRaw(check_plain($this->node1->name));
  }

  /**
   * Test Hide source when target is empty condition on DS
   */
  function DSTestHideSourceTargetIfEmpty() {

    // Hide submitted_by when body is empty.
    $condition = array(
      'fields[submitted_by][settings_edit_form][settings][conditions][0][condition]' => 'hide_if_empty',
    );
    $configuration = array(
      'fields[submitted_by][settings_edit_form][settings][conditions][0][configuration][target]' => 'body',
    );
    $this
      ->ffcEditFormatterSettings($condition, $configuration, 'edit submitted_by');

    // Insert the original node/1 body value back into the node/1 body
    $edit = array(
      'body[und][0][value]' => $nodeBodyValue,
    );
    $this
      ->drupalPost('node/' . $this->node1->nid . '/edit', $edit, t('Save'));
    $body_value = $this->node1->body[$this->node1->language][0]['value'];
    $this
      ->assertText($body_value);
    $this
      ->assertRaw(check_plain($this->node1->name));

    // Empty body.
    $edit = array(
      'body[und][0][value]' => '',
    );
    $this
      ->drupalPost('node/' . $this->node1->nid . '/edit', $edit, t('Save'));
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertNoText($body_value);
    $this
      ->assertNoRaw(check_plain($this->node1->name));
  }

  /**
   * Test Hide source when current user has certain roles on DS.
   */
  function DSTestHideSourceUserRoles() {

    // Hide submitted_by when user is anonymous.
    $condition = array(
      'fields[submitted_by][settings_edit_form][settings][conditions][0][condition]' => 'hide_on_role',
    );
    $configuration = array(
      'fields[submitted_by][settings_edit_form][settings][conditions][0][configuration][roles][]' => '1',
    );
    $this
      ->ffcEditFormatterSettings($condition, $configuration, 'edit submitted_by');

    // Verify the post date is still there for the authenticated user.
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertRaw(check_plain($this->node1->name));

    // Log out and verify post date is not seen.
    $this
      ->drupalLogout();
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertNoRaw(check_plain($this->node1->name));

    // Login in again.
    $this
      ->drupalLogin($this->admin_user);
  }

  /**
   * Test Hide source on specific pages on DS.
   */
  function DSTestHideSourcePages() {

    // Hide fields on specific pages.
    $condition = array(
      'fields[submitted_by][settings_edit_form][settings][conditions][0][condition]' => 'hide_on_pages',
    );
    $configuration = array(
      'fields[submitted_by][settings_edit_form][settings][conditions][0][configuration][pages]' => 'node/1/testing',
      'fields[submitted_by][settings_edit_form][settings][conditions][0][configuration][visibility]' => '1',
    );
    $this
      ->ffcEditFormatterSettings($condition, $configuration, 'edit submitted_by');

    // Verify the post date is on node/1.
    $this
      ->drupalGet('node/' . $this->node1->nid);
    $this
      ->assertRaw(check_plain($this->node1->name));

    // Verify the post date is not on node/1/testing/
    $this
      ->drupalGet('node/' . $this->node1->nid . '/testing');
    $this
      ->assertNoRaw(check_plain($this->node1->name));
  }

}

Classes

Namesort descending Description
FFCTestCase Group UI tests.