View source
<?php
namespace Drupal\Tests\views_ui\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\views\Entity\View;
class ViewEditTest extends UITestBase {
public static $testViews = [
'test_view',
'test_display',
'test_groupwise_term_ui',
];
protected $defaultTheme = 'stark';
public function testDeleteLink() {
$this
->drupalGet('admin/structure/views/view/test_view');
$this
->assertSession()
->linkExists(t('Delete view'), 0, 'Ensure that the view delete link appears');
$view = $this->container
->get('entity_type.manager')
->getStorage('view')
->load('test_view');
$this
->assertInstanceOf(View::class, $view);
$this
->clickLink(t('Delete view'));
$this
->assertUrl('admin/structure/views/view/test_view/delete');
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertRaw(t('The view %name has been deleted.', [
'%name' => $view
->label(),
]));
$this
->assertUrl('admin/structure/views');
$view = $this->container
->get('entity_type.manager')
->getStorage('view')
->load('test_view');
$this
->assertNotInstanceOf(View::class, $view);
}
public function testOtherOptions() {
$this
->drupalGet('admin/structure/views/view/test_view');
$this
->drupalPostForm(NULL, [], 'Add Attachment');
$edit = [
'display_comment' => 'one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen',
];
$this
->drupalPostForm('admin/structure/views/nojs/display/test_view/attachment_1/display_comment', $edit, 'Apply');
$this
->assertText('one two three four five six seven eight nine ten eleven twelve thirteen fourteen...');
$edit = [
'display_id' => 'test_1',
];
$this
->drupalPostForm('admin/structure/views/nojs/display/test_view/attachment_1/display_id', $edit, 'Apply');
$this
->assertSession()
->linkExists(t('test_1'));
$this
->drupalPostForm(NULL, [], 'Save');
$view = \Drupal::entityTypeManager()
->getStorage('view')
->load('test_view');
$displays = $view
->get('display');
$this
->assertTrue(!empty($displays['test_1']), 'Display data found for new display ID key.');
$this
->assertIdentical($displays['test_1']['id'], 'test_1', 'New display ID matches the display ID key.');
$this
->assertArrayNotHasKey('attachment_1', $displays);
$edit = [
'display_id' => 'test_1',
];
$this
->drupalPostForm('admin/structure/views/nojs/display/test_view/test_1/display_id', $edit, 'Apply');
$this
->drupalPostForm(NULL, [], 'Save');
$this
->assertSession()
->linkExists(t('test_1'));
$machine_name_edit_url = 'admin/structure/views/nojs/display/test_view/test_1/display_id';
$error_text = t('Display machine name must contain only lowercase letters, numbers, or underscores.');
try {
$this
->drupalGet('admin/structure/views/ajax/handler/test_view/fake_display_name/filter/title');
$this
->fail('Expected error, when setDisplay() called with invalid display ID');
} catch (\Exception $e) {
$this
->assertStringContainsString('setDisplay() called with invalid display ID "fake_display_name".', $e
->getMessage());
}
$edit = [
'display_id' => 'test 1',
];
$this
->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
$this
->assertText($error_text);
$edit = [
'display_id' => 'test_1#',
];
$this
->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
$this
->assertText($error_text);
$edit = [
'display_id' => 'default',
];
$this
->drupalPostForm($machine_name_edit_url, $edit, 'Apply');
$this
->assertText(t('Display id should be unique.'));
$this
->drupalGet('admin/structure/views/view/test_view/edit/test_1');
$this
->assertSession()
->linkExists(t('test_1'));
$this
->drupalGet('admin/structure/views/view/test_view');
$fields = [];
$fields['fields[age][removed]'] = 1;
$fields['fields[id][removed]'] = 1;
$fields['fields[name][removed]'] = 1;
$this
->drupalPostForm('admin/structure/views/nojs/rearrange/test_view/default/field', $fields, t('Apply'));
$this
->drupalPostForm(NULL, [], 'Save');
$this
->drupalPostForm(NULL, [], t('Cancel'));
$this
->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.');
$this
->assertUrl('admin/structure/views', [], 'Redirected back to the view listing page..');
}
public function testEditFormLanguageOptions() {
$assert_session = $this
->assertSession();
$test_views = [
'test_view' => 'default',
'test_display' => 'page_1',
];
foreach ($test_views as $view_name => $display) {
$this
->drupalGet('admin/structure/views/view/' . $view_name);
$this
->assertSession()
->statusCodeEquals(200);
$langcode_url = 'admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language';
$this
->assertNoLinkByHref($langcode_url);
$assert_session
->linkNotExistsExact(t('@type language selected for page', [
'@type' => t('Content'),
]));
$this
->assertSession()
->linkNotExists(t('Content language of view row'));
}
$this->container
->get('module_installer')
->install([
'language',
'content_translation',
]);
ConfigurableLanguage::createFromLangcode('hu')
->save();
$this
->resetAll();
$this
->rebuildContainer();
foreach ($test_views as $view_name => $display) {
$this
->drupalGet('admin/structure/views/view/' . $view_name);
$this
->assertSession()
->statusCodeEquals(200);
$langcode_url = 'admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language';
if ($view_name == 'test_view') {
$this
->assertNoLinkByHref($langcode_url);
$assert_session
->linkNotExistsExact(t('@type language selected for page', [
'@type' => t('Content'),
]));
$this
->assertSession()
->linkNotExists(t('Content language of view row'));
}
else {
$this
->assertLinkByHref($langcode_url);
$assert_session
->linkNotExistsExact(t('@type language selected for page', [
'@type' => t('Content'),
]));
$this
->assertSession()
->linkExists(t('Content language of view row'));
}
$this
->drupalGet($langcode_url);
$this
->assertSession()
->statusCodeEquals(200);
if ($view_name == 'test_view') {
$this
->assertText(t('The view is not based on a translatable entity type or the site is not multilingual.'));
}
else {
$this
->assertFieldByName('rendering_language', '***LANGUAGE_entity_translation***');
$expected_elements = [
'***LANGUAGE_entity_translation***',
'***LANGUAGE_entity_default***',
'***LANGUAGE_site_default***',
'***LANGUAGE_language_interface***',
'en',
'hu',
];
$elements = $this
->xpath('//select[@id="edit-rendering-language"]/option');
for ($i = 0; $i < count($elements); $i++) {
$this
->assertEqual($elements[$i]
->getAttribute('value'), $expected_elements[$i]);
}
$edit = [
'authenticated[administer languages]' => TRUE,
];
$this
->drupalPostForm('/admin/people/permissions', $edit, t('Save permissions'));
$edit = [
'language_content[configurable]' => TRUE,
];
$this
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$edit = [
'rendering_language' => '***LANGUAGE_language_content***',
];
$this
->drupalPostForm('/admin/structure/views/nojs/display/' . $view_name . '/' . $display . '/rendering_language', $edit, t('Apply'));
$edit = [
'language_content[configurable]' => FALSE,
];
$this
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$this
->drupalGet($langcode_url);
$element = $this
->xpath('//select[@id="edit-rendering-language"]/option[@value="***LANGUAGE_language_content***" and @selected="selected"]');
$this
->assertFalse(empty($element), 'Current selection is not lost');
$langcode_url = 'admin/structure/views/nojs/handler/' . $view_name . '/' . $display . '/filter/langcode';
$this
->drupalGet($langcode_url);
$this
->assertSession()
->statusCodeEquals(200);
$expected_elements = [
'all',
'***LANGUAGE_site_default***',
'***LANGUAGE_language_interface***',
'***LANGUAGE_language_content***',
'en',
'hu',
'und',
'zxx',
];
$elements = $this
->xpath('//div[@id="edit-options-value"]//input');
for ($i = 0; $i < count($elements); $i++) {
$this
->assertEqual($elements[$i]
->getAttribute('value'), $expected_elements[$i]);
}
}
}
}
public function testRelationRepresentativeNode() {
$edit["name[taxonomy_term_field_data.tid_representative]"] = TRUE;
$this
->drupalPostForm('admin/structure/views/nojs/add-handler/test_groupwise_term_ui/default/relationship', $edit, 'Add and configure relationships');
$edit = [];
$this
->drupalPostForm('admin/structure/views/nojs/handler/test_groupwise_term_ui/default/relationship/tid_representative', $edit, 'Apply');
}
}