View source
<?php
namespace Drupal\Tests\views_ui\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class DefaultViewsTest extends UITestBase {
public static $testViews = [
'test_view_status',
'test_page_display_menu',
'test_page_display_arguments',
];
protected $defaultTheme = 'stark';
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->placeBlock('page_title_block');
}
public function testDefaultViews() {
$edit_href = 'admin/structure/views/view/glossary';
$this
->drupalGet('admin/structure/views');
$this
->drupalGet('admin/structure/views');
$this
->clickViewsOperationLink('Enable', '/glossary/');
$this
->assertSession()
->addressEquals('admin/structure/views');
$this
->assertSession()
->linkByHrefExists($edit_href);
$new_title = $this
->randomMachineName(16);
$edit = [
'title' => $new_title,
];
$this
->drupalGet('admin/structure/views/nojs/display/glossary/page_1/title');
$this
->submitForm($edit, 'Apply');
$this
->drupalGet('admin/structure/views/view/glossary/edit/page_1');
$this
->submitForm([], 'Save');
$this
->drupalGet('glossary');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($new_title);
$this
->drupalGet('admin/structure/views/nojs/display/archive/page_1/title');
$this
->submitForm([], 'Apply');
$this
->drupalGet('admin/structure/views/view/archive/edit/page_1');
$this
->submitForm([], 'Save');
$this
->drupalGet('admin/structure/views');
$this
->assertSession()
->linkByHrefExists('admin/structure/views/view/archive/enable');
$this
->clickViewsOperationLink('Enable', '/archive/');
$this
->drupalGet('admin/structure/views');
$this
->clickViewsOperationLink('Duplicate', '/glossary');
$edit = [
'id' => 'duplicate_of_glossary',
];
$this
->assertSession()
->titleEquals('Duplicate of Glossary | Drupal');
$this
->submitForm($edit, 'Duplicate');
$this
->assertSession()
->addressEquals('admin/structure/views/view/duplicate_of_glossary');
$this
->drupalGet('admin/structure/views');
$this
->clickViewsOperationLink('Duplicate', '/glossary');
$random_name = strtolower($this
->randomMachineName());
$this
->submitForm([
'id' => $random_name,
], 'Duplicate');
$this
->assertSession()
->addressEquals("admin/structure/views/view/{$random_name}");
$this
->drupalGet('admin/structure/views');
$this
->clickViewsOperationLink('Disable', '/glossary/');
$this
->drupalGet('admin/structure/views');
$this
->clickViewsOperationLink('Enable', '/glossary/');
$this
->assertSession()
->addressEquals('admin/structure/views');
$this
->assertSession()
->linkByHrefExists($edit_href);
Role::load(RoleInterface::ANONYMOUS_ID)
->revokePermission('access content')
->save();
$this
->drupalLogout();
$this
->drupalGet('glossary');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('archive');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->fullAdminUser);
$this
->drupalGet('admin/structure/views');
$this
->clickViewsOperationLink('Delete', '/glossary/');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->addressEquals('admin/structure/views');
$this
->assertSession()
->linkByHrefNotExists($edit_href);
$this
->drupalGet($edit_href);
$this
->assertSession()
->statusCodeEquals(404);
$this
->assertSession()
->pageTextContains('Page not found');
$this
->drupalGet('admin/structure/views');
$this
->clickViewsOperationLink('Delete', 'duplicate_of_glossary');
$this
->submitForm([], 'Delete');
$this
->drupalGet('glossary');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/structure/views');
$this
->clickViewsOperationLink('Delete', $random_name);
$this
->submitForm([], 'Delete');
$this
->drupalGet('glossary');
$this
->assertSession()
->statusCodeEquals(404);
$this
->assertSession()
->pageTextContains('Page not found');
}
public function testSplitListing() {
$xpath = '//div[@id="views-entity-list"]/div[@class = :status]/table//td/text()[contains(., :title)]';
$arguments = [
':status' => 'views-list-section enabled',
':title' => 'test_view_status',
];
$this
->drupalGet('admin/structure/views');
$elements = $this
->xpath($xpath, $arguments);
$this
->assertCount(0, $elements, 'A disabled view is not found in the enabled views table.');
$arguments[':status'] = 'views-list-section disabled';
$elements = $this
->xpath($xpath, $arguments);
$this
->assertCount(1, $elements, 'A disabled view is found in the disabled views table.');
$this
->clickViewsOperationLink('Enable', '/test_view_status/');
$elements = $this
->xpath($xpath, $arguments);
$this
->assertCount(0, $elements, 'After enabling a view, it is not found in the disabled views table.');
$arguments[':status'] = 'views-list-section enabled';
$elements = $this
->xpath($xpath, $arguments);
$this
->assertCount(1, $elements, 'After enabling a view, it is found in the enabled views table.');
$this
->drupalGet('admin/structure/views/view/test_view_status/disable');
$this
->assertSession()
->statusCodeEquals(403);
}
public function testPathDestination() {
$this
->drupalGet('admin/structure/views');
$this
->assertSession()
->linkByHrefExists('test_page_display_menu');
$this
->assertSession()
->linkByHrefNotExists('test_page_display_menu/default');
$this
->assertSession()
->linkByHrefExists('test_page_display_menu/local');
$this
->assertSession()
->responseContains('test_route_with_suffix/%/suffix');
$this
->assertSession()
->linkByHrefNotExists(Url::fromUri('base:test_route_with_suffix/%/suffix')
->toString());
}
public function clickViewsOperationLink($label, $unique_href_part) {
$links = $this
->xpath('//a[normalize-space(text())=:label]', [
':label' => (string) $label,
]);
foreach ($links as $link_index => $link) {
$position = strpos($link
->getAttribute('href'), $unique_href_part);
if ($position !== FALSE) {
$index = $link_index;
break;
}
}
$this
->assertTrue(isset($index), new FormattableMarkup('Link to "@label" containing @part found.', [
'@label' => $label,
'@part' => $unique_href_part,
]));
if (isset($index)) {
return $this
->clickLink((string) $label, $index);
}
else {
return FALSE;
}
}
}