View source
<?php
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;
class ExtensionViewsFieldTest extends ViewKernelTestBase {
public static $modules = array(
'file',
'file_test_views',
'user',
);
public static $testViews = array(
'file_extension_view',
);
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();
}
public function testFileExtensionTarOption() {
$renderer = \Drupal::service('renderer');
$view = Views::getView('file_extension_view');
$view
->setDisplay();
$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]), 'gz');
$this
->assertEqual($view->field['extension']
->advancedRender($view->result[3]), '');
});
$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]), '');
});
}
}