AreaTest.php

Definition of Drupal\views\Tests\Handler\AreaTest.

Namespace

Drupal\views\Tests\Handler

File

drupal/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTest.php
View source
<?php

/**
 * @file
 * Definition of Drupal\views\Tests\Handler\AreaTest.
 */
namespace Drupal\views\Tests\Handler;


/**
 * Tests the abstract area handler.
 *
 * @see Drupal\views\Plugin\views\area\AreaPluginBase
 * @see Drupal\views_test\Plugin\views\area\TestExample
 */
class AreaTest extends HandlerTestBase {

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array(
    'test_example_area',
  );

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'views_ui',
  );
  public static function getInfo() {
    return array(
      'name' => 'Area: Base',
      'description' => 'Test the plugin base of the area handler.',
      'group' => 'Views Handlers',
    );
  }
  protected function setUp() {
    parent::setUp();
    $this
      ->enableViewsTestModule();
  }
  protected function viewsData() {
    $data = parent::viewsData();
    $data['views']['test_example'] = array(
      'title' => 'Test Example area',
      'help' => 'A area handler which just exists for tests.',
      'area' => array(
        'id' => 'test_example',
      ),
    );
    return $data;
  }

  /**
   * Tests the generic UI of a area handler.
   */
  public function testUI() {
    $admin_user = $this
      ->drupalCreateUser(array(
      'administer views',
      'administer site configuration',
    ));
    $this
      ->drupalLogin($admin_user);
    $types = array(
      'header',
      'footer',
      'empty',
    );
    $labels = array();
    foreach ($types as $type) {
      $edit_path = 'admin/structure/views/nojs/config-item/test_example_area/default/' . $type . '/test_example';

      // First setup an empty label.
      $this
        ->drupalPost($edit_path, array(), t('Apply'));
      $this
        ->assertText('Test Example area');

      // Then setup a no empty label.
      $labels[$type] = $this
        ->randomName();
      $this
        ->drupalPost($edit_path, array(
        'options[admin_label]' => $labels[$type],
      ), t('Apply'));

      // Make sure that the new label appears on the site.
      $this
        ->assertText($labels[$type]);

      // Test that the settings (empty/admin_label) are accessible.
      $this
        ->drupalGet($edit_path);
      $this
        ->assertField('options[admin_label]');
      if ($type !== 'empty') {
        $this
          ->assertField('options[empty]');
      }
    }
  }

  /**
   * Tests the rendering of an area.
   */
  public function testRenderArea() {
    $view = views_get_view('test_example_area');
    $view
      ->initHandlers();

    // Insert a random string to the test area plugin and see whether it is
    // rendered for both header, footer and empty text.
    $header_string = $this
      ->randomString();
    $footer_string = $this
      ->randomString();
    $empty_string = $this
      ->randomString();
    $view->header['test_example']->options['string'] = $header_string;
    $view->header['test_example']->options['empty'] = TRUE;
    $view->footer['test_example']->options['string'] = $footer_string;
    $view->footer['test_example']->options['empty'] = TRUE;
    $view->empty['test_example']->options['string'] = $empty_string;

    // Check whether the strings exists in the output.
    $output = $view
      ->preview();
    $output = drupal_render($output);
    $this
      ->assertTrue(strpos($output, $header_string) !== FALSE);
    $this
      ->assertTrue(strpos($output, $footer_string) !== FALSE);
    $this
      ->assertTrue(strpos($output, $empty_string) !== FALSE);
  }

  /**
   * Tests global tokens.
   */
  public function testRenderAreaToken() {
    $admin_user = $this
      ->drupalCreateUser(array(
      'administer views',
      'administer site configuration',
    ));
    $this
      ->drupalLogin($admin_user);
    $view = views_get_view('test_example_area');
    $view
      ->initHandlers();
    $this
      ->drupalGet('admin/structure/views/nojs/config-item/test_example_area/default/empty/test_example');

    // Test that the list is token present.
    $element = $this
      ->xpath('//ul[@class="global-tokens"]');
    $this
      ->assertTrue($element, 'Token list found on the options form.');
    $empty_handler =& $view->empty['test_example'];

    // Test the list of available tokens.
    $available = $empty_handler
      ->getAvailableGlobalTokens();
    foreach (array(
      'site',
      'view',
    ) as $type) {
      $this
        ->assertTrue(!empty($available[$type]) && is_array($available[$type]));

      // Test that each item exists in the list.
      foreach ($available[$type] as $token => $info) {
        $this
          ->assertText("[{$type}:{$token}]");
      }
    }

    // Test the rendered output of a token.
    $empty_handler->options['string'] = '[site:name]';

    // Test we have the site:name token in the output.
    $output = $view
      ->preview();
    $output = drupal_render($output);
    $expected = \Drupal::token()
      ->replace('[site:name]');
    $this
      ->assertTrue(strpos($output, $expected) !== FALSE);
  }

  /**
   * Tests overriding the view title using the area title handler.
   */
  public function testTitleArea() {
    $view = views_get_view('frontpage');
    $view
      ->initDisplay('page_1');

    // Add the title area handler to the empty area.
    $view->displayHandlers
      ->get('page_1')
      ->overrideOption('empty', array(
      'title' => array(
        'id' => 'title',
        'table' => 'views',
        'field' => 'title',
        'admin_label' => '',
        'empty' => '0',
        'title' => 'Overridden title',
      ),
    ));
    $view->storage
      ->enable()
      ->save();
    $this
      ->drupalGet('node');
    $this
      ->assertText('Overridden title', 'Overridden title found.');
  }

}

Classes

Namesort descending Description
AreaTest Tests the abstract area handler.