You are here

ExtensionViewsFieldTest.php in Zircon Profile 8.0

Same filename and directory in other branches
  1. 8 core/modules/file/src/Tests/Views/ExtensionViewsFieldTest.php

File

core/modules/file/src/Tests/Views/ExtensionViewsFieldTest.php
View source
<?php

/**
 * @file
 * Contains \Drupal\file\Tests\Views\ExtensionViewsFieldTest.
 */
namespace Drupal\file\Tests\Views;

use Drupal\Core\Render\RenderContext;
use Drupal\file\Entity\File;
use Drupal\views\Views;
use Drupal\views\Tests\ViewKernelTestBase;
use Drupal\views\Tests\ViewTestData;

/**
 * Tests the core Drupal\file\Plugin\views\field\Extension handler.
 *
 * @group file
 */
class ExtensionViewsFieldTest extends ViewKernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = array(
    'file',
    'file_test_views',
    'user',
  );

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    ViewTestData::createTestViews(get_class($this), array(
      'file_test_views',
    ));
    $this
      ->installEntitySchema('file');
    file_put_contents('public://file.png', '');
    File::create([
      'uri' => 'public://file.png',
      'filename' => 'file.png',
    ])
      ->save();
    file_put_contents('public://file.tar', '');
    File::create([
      'uri' => 'public://file.tar',
      'filename' => 'file.tar',
    ])
      ->save();
    file_put_contents('public://file.tar.gz', '');
    File::create([
      'uri' => 'public://file.tar.gz',
      'filename' => 'file.tar.gz',
    ])
      ->save();
    file_put_contents('public://file', '');
    File::create([
      'uri' => 'public://file',
      'filename' => 'file',
    ])
      ->save();
  }

  /**
   * Tests file extension views field handler extension_detect_tar option.
   */
  public function testFileExtensionTarOption() {

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $view = Views::getView('file_extension_view');
    $view
      ->setDisplay();
    $this
      ->executeView($view);

    // Test without the tar option.
    $renderer
      ->executeInRenderContext(new RenderContext(), function () use ($view) {
      $this
        ->assertEqual($view->field['extension']
        ->advancedRender($view->result[0]), 'png');
      $this
        ->assertEqual($view->field['extension']
        ->advancedRender($view->result[1]), 'tar');
      $this
        ->assertEqual($view->field['extension']
        ->advancedRender($view->result[2]), 'gz');
      $this
        ->assertEqual($view->field['extension']
        ->advancedRender($view->result[3]), '');
    });

    // Test with the tar option.
    $view = Views::getView('file_extension_view');
    $view
      ->setDisplay();
    $view
      ->initHandlers();
    $view->field['extension']->options['settings']['extension_detect_tar'] = TRUE;
    $this
      ->executeView($view);
    $renderer
      ->executeInRenderContext(new RenderContext(), function () use ($view) {
      $this
        ->assertEqual($view->field['extension']
        ->advancedRender($view->result[0]), 'png');
      $this
        ->assertEqual($view->field['extension']
        ->advancedRender($view->result[1]), 'tar');
      $this
        ->assertEqual($view->field['extension']
        ->advancedRender($view->result[2]), 'tar.gz');
      $this
        ->assertEqual($view->field['extension']
        ->advancedRender($view->result[3]), '');
    });
  }

}

Classes

Namesort descending Description
ExtensionViewsFieldTest Tests the core Drupal\file\Plugin\views\field\Extension handler.