You are here

empty_fields.test in Empty fields 7.2

Tests for the empty fields module.

File

tests/empty_fields.test
View source
<?php

/**
 * @file
 * Tests for the empty fields module.
 */

/**
 * Helper test class with some added functions for testing.
 */
class EmptyFieldsViewModesWebTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Empty Fields View Modes',
      'description' => 'Test the empty text is correctly added per view mode.',
      'group' => 'Empty Fields',
    );
  }
  public function setUp() {
    parent::setUp(array(
      'field_test',
      'empty_fields',
    ));
    $this->webUser = $this
      ->drupalCreateUser(array(
      'access field_test content',
      'administer field_test content',
      'administer content types',
      'administer fields',
      'administer nodes',
      'bypass node access',
    ));
    $this
      ->drupalLogin($this->webUser);
  }

  /**
   * Tests that the module actually works.
   */
  public function testEmptyFieldsOutput() {
    $edit = array(
      'status' => 1,
      'promote' => 1,
      'title' => $this
        ->randomName(8),
    );
    $this
      ->drupalPost("node/add/page", $edit, t('Save'));
    $node = $this
      ->drupalGetNodeByTitle($edit['title']);

    // Checks full and teaser display modes via default.
    $edit = array(
      'view_modes_custom[teaser]' => FALSE,
    );
    $this
      ->drupalPost('admin/structure/types/manage/page/display', $edit, t('Save'));
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText($node->title);
    $this
      ->drupalGet('node');
    $this
      ->assertText($node->title);

    // Enable the default setting to use empty text settings.
    $instance_info = field_info_instance('node', 'body', 'page');
    $instance_info['display']['default']['settings']['empty_fields_handler'] = 'EmptyFieldText';
    $instance_info['display']['default']['settings']['empty_fields_emptyfieldtext_empty_text'] = 'foo bar';
    $instance_info['display']['default']['settings']['empty_fields_empty_class'] = 'fb-class';
    field_update_instance($instance_info);

    // Checks full and teaser display modes.
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText($node->title);
    $this
      ->assertText('foo bar');
    $this
      ->drupalGet('admin/structure/types/manage/page/display');
    $this
      ->drupalGet('admin/structure/types/manage/page/display/teaser');
    $this
      ->drupalGet('node');
    $this
      ->assertText($node->title);
    $this
      ->assertText('foo bar');

    // Enable and configure the teaser display.
    $edit = array(
      'view_modes_custom[full]' => 'full',
      'view_modes_custom[teaser]' => 'teaser',
    );
    $this
      ->drupalPost('admin/structure/types/manage/page/display', $edit, t('Save'));
    $instance_info = field_info_instance('node', 'body', 'page');
    $instance_info['display']['teaser']['settings']['empty_fields_handler'] = 'EmptyFieldText';
    $instance_info['display']['teaser']['settings']['empty_fields_emptyfieldtext_empty_text'] = 'bar foo';
    $instance_info['display']['teaser']['settings']['empty_fields_empty_class'] = 'bf-class';
    field_update_instance($instance_info);
    $instance_info = field_info_instance('node', 'body', 'page');
    $instance_info['display']['full']['settings']['empty_fields_handler'] = '';
    field_update_instance($instance_info);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText($node->title);
    $this
      ->assertNoText('foo bar');
    $this
      ->assertNoText('bar foo');
    $this
      ->drupalGet('admin/structure/types/manage/page/display');
    $this
      ->drupalGet('admin/structure/types/manage/page/display/teaser');
    $this
      ->drupalGet('node');
    $this
      ->assertText($node->title);
    $this
      ->assertText('bar foo');
  }

  /**
   * Test multiple view modes work.
   */
  public function testEmptyFieldsWithMultipleViewModes() {
    $edit = array(
      'status' => 1,
      'promote' => 1,
      'title' => $this
        ->randomName(8),
    );
    $this
      ->drupalPost("node/add/page", $edit, t('Save'));
    $node = $this
      ->drupalGetNodeByTitle($edit['title']);
    $edit = array(
      'view_modes_custom[full]' => 'full',
      'view_modes_custom[teaser]' => 'teaser',
    );
    $this
      ->drupalPost('admin/structure/types/manage/page/display', $edit, t('Save'));
    $instance_info = field_info_instance('node', 'body', 'page');
    $instance_info['display']['default']['settings']['empty_fields_handler'] = 'EmptyFieldText';
    $instance_info['display']['default']['settings']['empty_fields_emptyfieldtext_empty_text'] = 'foo bar 1';
    $instance_info['display']['default']['settings']['empty_fields_empty_class'] = 'fb-class-1';
    $instance_info['display']['full']['settings']['empty_fields_handler'] = 'EmptyFieldText';
    $instance_info['display']['full']['settings']['empty_fields_emptyfieldtext_empty_text'] = 'foo bar 2';
    $instance_info['display']['full']['settings']['empty_fields_empty_class'] = 'fb-class-2';
    $instance_info['display']['teaser']['settings']['empty_fields_handler'] = 'EmptyFieldText';
    $instance_info['display']['teaser']['settings']['empty_fields_emptyfieldtext_empty_text'] = 'foo bar 3';
    $instance_info['display']['teaser']['settings']['empty_fields_empty_class'] = 'fb-class-3';
    field_update_instance($instance_info);
    $output = '';
    $langcode = field_language('node', $node, 'body');
    field_attach_prepare_view('node', array(
      $node->nid => $node,
    ), 'default', $langcode);
    entity_prepare_view('node', array(
      $node->nid => $node,
    ), $langcode);
    $build = field_attach_view('node', $node, 'default', $langcode);
    $output .= drupal_render($build);
    unset($node->_field_view_prepared);
    field_attach_prepare_view('node', array(
      $node->nid => $node,
    ), 'full', $langcode);
    entity_prepare_view('node', array(
      $node->nid => $node,
    ), $langcode);
    $build = field_attach_view('node', $node, 'full', $langcode);
    $output .= drupal_render($build);
    unset($node->_field_view_prepared);
    field_attach_prepare_view('node', array(
      $node->nid => $node,
    ), 'teaser', $langcode);
    entity_prepare_view('node', array(
      $node->nid => $node,
    ), $langcode);
    $build = field_attach_view('node', $node, 'teaser', $langcode);
    $output .= drupal_render($build);
    $this
      ->verbose($output);
    $this
      ->assertTrue(strpos($output, 'foo bar 1'));
    $this
      ->assertTrue(strpos($output, 'foo bar 2'));
    $this
      ->assertTrue(strpos($output, 'foo bar 3'));
  }

}

Classes

Namesort descending Description
EmptyFieldsViewModesWebTestCase Helper test class with some added functions for testing.