ViewTranslationTest.php in Drupal 9
File
core/modules/views/tests/src/Functional/ViewTranslationTest.php
View source
<?php
namespace Drupal\Tests\views\Functional;
use Drupal\Component\Utility\Xss;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\views\Views;
class ViewTranslationTest extends ViewTestBase {
public static $testViews = [
'test_view',
];
protected static $modules = [
'block',
'locale',
'language',
'config_translation',
'views_ui',
];
protected $defaultTheme = 'stark';
public function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->enableViewsTestModule();
$this
->drupalPlaceBlock('system_breadcrumb_block');
ConfigurableLanguage::createFromLangcode('nl')
->save();
$config = $this
->config('system.performance');
$config
->set('cache.page.max_age', 3600);
$config
->save();
}
public function testViewTitleTranslation() {
$view = Views::getView('test_view');
$view->storage
->addDisplay('page');
$displays = $view->storage
->get('display');
$displays['default']['display_options']['title'] = 'Title EN';
$displays['page_1']['display_options']['path'] = 'test-view';
$view->storage
->set('display', $displays);
$view
->save();
\Drupal::service('router.builder')
->rebuild();
$admin_user = $this
->drupalCreateUser([
'translate configuration',
]);
$this
->drupalLogin($admin_user);
$edit = [
'translation[config_names][views.view.test_view][display][default][display_options][title]' => 'Titel NL',
];
$this
->drupalGet('admin/structure/views/view/test_view/translate/nl/edit');
$this
->submitForm($edit, 'Save translation');
$this
->drupalLogout();
$this
->drupalGet('test-view');
$this
->assertSession()
->titleEquals('Title EN | Drupal');
$this
->assertEquals('MISS', $this
->getSession()
->getResponseHeader('X-Drupal-Cache'));
$this
->drupalGet('test-view');
$this
->assertEquals('HIT', $this
->getSession()
->getResponseHeader('X-Drupal-Cache'));
$this
->drupalGet('test-view/not-relevant');
$this
->assertSession()
->linkExists('Title EN');
$this
->assertSession()
->linkNotExists('Titel NL');
$this
->drupalGet('nl/test-view');
$this
->assertSession()
->titleEquals('Titel NL | Drupal');
$this
->assertEquals('MISS', $this
->getSession()
->getResponseHeader('X-Drupal-Cache'));
$this
->drupalGet('test-view');
$this
->assertEquals('HIT', $this
->getSession()
->getResponseHeader('X-Drupal-Cache'));
$this
->drupalGet('nl/test-view/not-relevant');
$this
->assertSession()
->linkExists('Titel NL');
$this
->assertSession()
->linkNotExists('Title EN');
$displays = $view->storage
->get('display');
$unsafe_title = 'This is an unsafe title <script>alert("click me!")</script>';
$safe_title = Xss::filter($unsafe_title);
$displays['default']['display_options']['title'] = $unsafe_title;
$view->storage
->set('display', $displays);
$view
->save();
$this
->drupalGet('test-view');
$this
->assertSession()
->titleEquals($safe_title . ' | Drupal');
}
}