View source
<?php
namespace Drupal\Tests\page_manager_ui\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\page_manager\Entity\Page;
use Drupal\page_manager\Entity\PageVariant;
use Drupal\Tests\BrowserTestBase;
class PageManagerAdminTest extends BrowserTestBase {
protected $defaultTheme = 'classy';
public static $modules = [
'block',
'page_manager_ui',
'page_manager_test',
];
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('system_branding_block');
$this
->drupalPlaceBlock('page_title_block');
\Drupal::service('theme_installer')
->install([
'bartik',
]);
$this
->config('system.theme')
->set('admin', 'classy')
->save();
$this
->drupalLogin($this
->drupalCreateUser([
'administer pages',
'access administration pages',
'view the administration theme',
]));
Page::load('node_view')
->delete();
}
public function testAdmin() {
$this
->doTestAddPage();
$this
->doTestAccessConditions();
$this
->doTestSelectionCriteria();
$this
->doTestDisablePage();
$this
->doTestAddVariant();
$this
->doTestAddBlock();
$this
->doTestSecondPage();
$this
->doTestEditBlock();
$this
->doTestEditVariant();
$this
->doTestReorderVariants();
$this
->doTestAddPageWithDuplicatePath();
$this
->doTestAdminPath();
$this
->doTestRemoveVariant();
$this
->doTestRemoveBlock();
$this
->doTestAddBlockWithAjax();
$this
->doTestEditBlock();
$this
->doTestExistingPathWithoutParameters();
$this
->doTestUpdateSubmit();
$this
->doTestDeletePage();
}
protected function doTestAddPage() {
$this
->drupalGet('admin/structure');
$this
->clickLink('Pages');
$this
->assertText('Add a new page.');
$this
->clickLink('Add page');
$edit = [
'id' => 'foo',
'path' => 'admin/foo',
'variant_plugin_id' => 'http_status_code',
'use_admin_theme' => TRUE,
'description' => 'This is our first test page.',
'wizard_options[access]' => TRUE,
'wizard_options[selection]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Next');
$this
->assertText('Administrative title field is required.');
$edit += [
'label' => 'Foo',
];
$this
->drupalPostForm(NULL, $edit, 'Next');
$this
->assertTitle('Page access | Drupal');
$access_path = 'admin/structure/page_manager/add/foo/access';
$this
->assertUrl($access_path . '?js=nojs');
$this
->doTestAccessConditions($access_path, FALSE);
$this
->drupalPostForm(NULL, [], 'Next');
$this
->assertTitle('Selection criteria | Drupal');
$selection_path = 'admin/structure/page_manager/add/foo/selection';
$this
->assertUrl($selection_path . '?js=nojs');
$this
->doTestSelectionCriteria($selection_path, FALSE);
$this
->drupalPostForm(NULL, [], 'Next');
$edit = [
'page_variant_label' => 'Status Code',
'variant_settings[status_code]' => 200,
];
$this
->drupalPostForm(NULL, $edit, 'Finish');
$this
->assertRaw(new FormattableMarkup('The page %label has been added.', [
'%label' => 'Foo',
]));
$this
->drupalGet('admin/structure/page_manager/manage/foo/general');
$this
->drupalGet('admin/foo');
$this
->assertResponse(200);
$this
->assertTitle('Foo | Drupal');
$this
->drupalGet('admin/structure/page_manager/manage/foo/page_variant__foo-http_status_code-0__general');
$edit = [
'variant_settings[status_code]' => 403,
];
$this
->drupalPostForm(NULL, $edit, 'Update');
$this
->drupalGet('admin/structure/page_manager/manage/foo/reorder_variants');
$edit = [
'variants[foo-http_status_code-0][weight]' => 10,
];
$this
->drupalPostForm(NULL, $edit, 'Update');
$this
->drupalPostForm(NULL, [], 'Update and save');
}
protected function doTestAccessConditions($path = 'admin/structure/page_manager/manage/foo/access', $redirect = TRUE) {
if ($this
->getUrl() !== $path && $redirect) {
$this
->drupalGet($path);
}
$this
->assertRaw('No required conditions have been configured.');
$edit = [
'conditions' => 'user_role',
];
$this
->drupalPostForm(NULL, $edit, 'Add Condition');
$this
->assertTitle('Add access condition | Drupal');
$edit = [
'roles[authenticated]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertRaw('The user is a member of Authenticated user');
$this
->assertUrl($path);
$this
->clickLink('Edit');
$this
->assertTitle('Edit access condition | Drupal');
$edit = [
'roles[anonymous]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertRaw('The user is a member of Anonymous user, Authenticated user');
$this
->assertUrl($path);
$this
->clickLink('Delete');
$this
->assertTitle('Are you sure you want to delete the user_role condition? | Drupal');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertRaw('No required conditions have been configured.');
$this
->assertUrl($path);
}
protected function doTestSelectionCriteria($path = 'admin/structure/page_manager/manage/foo/page_variant__foo-http_status_code-0__selection', $redirect = TRUE) {
if ($this
->getUrl() !== $path && $redirect) {
$this
->drupalGet($path);
}
$this
->assertRaw('No required conditions have been configured.');
$edit = [
'conditions' => 'user_role',
];
$this
->drupalPostForm(NULL, $edit, 'Add Condition');
$this
->assertTitle('Add new selection condition | Drupal');
$edit = [
'roles[authenticated]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertRaw('The user is a member of Authenticated user');
$this
->assertUrl($path);
$this
->clickLink('Edit');
$this
->assertTitle('Edit selection condition | Drupal');
$edit = [
'roles[anonymous]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertRaw('The user is a member of Anonymous user, Authenticated user');
$this
->assertUrl($path);
$this
->clickLink('Delete');
$this
->assertTitle('Are you sure you want to delete the user_role condition? | Drupal');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertRaw('No required conditions have been configured.');
$this
->assertUrl($path);
}
protected function doTestDisablePage() {
$this
->drupalGet('admin/foo');
$this
->assertResponse(403);
$this
->drupalGet('admin/structure/page_manager');
$this
->clickLink('Disable');
$this
->drupalGet('admin/foo');
$this
->assertResponse(404);
$this
->drupalGet('admin/structure/page_manager');
$this
->clickLink('Enable');
$this
->drupalGet('admin/foo');
$this
->assertResponse(403);
}
protected function doTestAddVariant() {
$this
->drupalGet('admin/structure/page_manager/manage/foo/general');
$this
->clickLink('Add variant');
$edit = [
'variant_plugin_id' => 'block_display',
'label' => 'First',
];
$this
->drupalPostForm(NULL, $edit, 'Next');
$edit = [
'variant_settings[page_title]' => 'Example title',
];
$this
->drupalPostForm(NULL, $edit, 'Next');
$this
->drupalPostForm(NULL, [], 'Finish');
$this
->drupalPostForm(NULL, [], 'Update and save');
$this
->drupalGet('admin/foo');
$this
->assertResponse(200);
$elements = $this
->getSession()
->getPage()
->findAll('css', 'div.region.region-content *');
if (version_compare(\Drupal::VERSION, '8.7', '<')) {
$this
->assertEmpty($elements);
}
else {
$this
->assertCount(1, $elements);
$this
->assertTrue($elements[0]
->hasAttribute('data-drupal-messages-fallback'));
}
}
protected function doTestAddBlock() {
$this
->drupalGet('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__content');
$this
->clickLink('Add new block');
$this
->assertNoText('Broken/Missing');
$this
->clickLink('User account menu');
$edit = [
'region' => 'top',
];
$this
->drupalPostForm(NULL, $edit, 'Add block');
$this
->drupalPostForm(NULL, [], 'Update and save');
$this
->drupalGet('admin/foo');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[@class="block-region-top"]/nav/ul[@class="menu"]/li/a');
$this
->assertTitle('Example title | Drupal');
$expected = [
'My account',
'Log out',
];
$links = [];
foreach ($elements as $element) {
$links[] = $element
->getText();
}
$this
->assertEqual($expected, $links);
$this
->assertRaw('User account menu');
}
protected function doTestSecondPage() {
$this
->drupalGet('admin/structure/page_manager');
$this
->clickLink('Add page');
$edit = [
'id' => 'second',
'label' => 'Second',
'path' => 'second',
'variant_plugin_id' => 'block_display',
];
$this
->drupalPostForm(NULL, $edit, 'Next');
$edit = [
'page_variant_label' => 'Second variant',
'variant_settings[page_title]' => 'Second title',
];
$this
->drupalPostForm(NULL, $edit, 'Next');
$this
->drupalPostForm(NULL, [], 'Finish');
$this
->assertRaw(new FormattableMarkup('The page %label has been added.', [
'%label' => 'Second',
]));
$this
->drupalGet('admin/foo');
$this
->assertTitle('Example title | Drupal');
$this
->drupalGet('second');
$this
->assertTitle('Second title | Drupal');
}
protected function doTestEditBlock() {
$this
->drupalGet('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__general');
$edit = [
'variant_settings[page_title]' => 'Updated block label',
'page_variant_label' => 'Updated block label',
];
$this
->drupalPostForm(NULL, $edit, 'Update and save');
$this
->drupalGet('admin/foo');
$this
->assertResponse(200);
$this
->assertRaw($edit['variant_settings[page_title]']);
}
protected function doTestEditVariant() {
if (!($block = $this
->findBlockByLabel('foo-block_display-0', 'User account menu'))) {
$this
->fail('Block not found');
return;
}
$block_config = $block
->getConfiguration();
$this
->drupalGet('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__content');
$this
->assertOptionSelected('edit-blocks-' . $block_config['uuid'] . '-region', 'top');
$this
->assertOptionSelected('edit-blocks-' . $block_config['uuid'] . '-weight', 0);
$form_name = 'blocks[' . $block_config['uuid'] . ']';
$edit = [
$form_name . '[region]' => 'bottom',
$form_name . '[weight]' => -10,
];
$this
->drupalPostForm(NULL, $edit, 'Update');
$this
->assertOptionSelected('edit-blocks-' . $block_config['uuid'] . '-region', 'bottom');
$this
->assertOptionSelected('edit-blocks-' . $block_config['uuid'] . '-weight', -10);
$this
->drupalPostForm(NULL, [], 'Update and save');
}
protected function doTestReorderVariants() {
$this
->drupalGet('admin/foo');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[@class="block-region-bottom"]/nav/ul[@class="menu"]/li/a');
$expected = [
'My account',
'Log out',
];
$links = [];
foreach ($elements as $element) {
$links[] = $element
->getText();
}
$this
->assertEqual($expected, $links);
$this
->drupalGet('admin/structure/page_manager/manage/foo/general');
$this
->clickLink('Reorder variants');
$edit = [
'variants[foo-http_status_code-0][weight]' => -10,
];
$this
->drupalPostForm(NULL, $edit, 'Update');
$this
->drupalPostForm(NULL, [], 'Update and save');
$this
->drupalGet('admin/foo');
$this
->assertResponse(403);
}
protected function doTestAddPageWithDuplicatePath() {
$edit = [
'label' => 'Bar',
'id' => 'bar',
'path' => 'admin/foo',
];
$this
->drupalPostForm('admin/structure/page_manager/add', $edit, 'Next');
$this
->assertText('The page path must be unique.');
$this
->drupalGet('admin/structure/page_manager');
$this
->assertNoText('Bar');
}
protected function doTestAdminPath() {
$this
->config('system.theme')
->set('default', 'bartik')
->save();
$this
->drupalGet('admin/foo');
$this
->assertTheme('classy');
$this
->drupalGet('admin/structure/page_manager/manage/foo/general');
$edit = [
'use_admin_theme' => FALSE,
];
$this
->drupalPostForm(NULL, $edit, 'Update and save');
$this
->drupalGet('admin/foo');
$this
->assertTheme('bartik');
$this
->config('system.theme')
->set('default', 'classy')
->save();
}
protected function doTestRemoveVariant() {
$this
->drupalGet('admin/structure/page_manager/manage/foo/page_variant__foo-http_status_code-0__general');
$this
->clickLink('Delete this variant');
$this
->assertRaw('Are you sure you want to delete this variant?');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertRaw(new FormattableMarkup('The variant %label has been removed.', [
'%label' => 'Status Code',
]));
$this
->drupalPostForm(NULL, [], 'Update and save');
}
protected function doTestRemoveBlock() {
$this
->drupalGet('admin/foo');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[@class="block-region-bottom"]/nav/ul[@class="menu"]/li/a');
$expected = [
'My account',
'Log out',
];
$links = [];
foreach ($elements as $element) {
$links[] = $element
->getText();
}
$this
->assertEqual($expected, $links);
$this
->drupalGet('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__content');
$this
->clickLink('Delete');
$this
->assertRaw(new FormattableMarkup('Are you sure you want to delete the block %label?', [
'%label' => 'User account menu',
]));
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertRaw(new FormattableMarkup('The block %label has been removed.', [
'%label' => 'User account menu',
]));
$this
->drupalPostForm(NULL, [], 'Update and save');
$this
->drupalGet('admin/foo');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[@class="block-region-bottom"]/nav/ul[@class="menu"]/li/a');
$this
->assertTrue(empty($elements));
}
protected function doTestAddBlockWithAjax() {
$this
->drupalGet('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__content');
$this
->clickLink('Add new block');
$this
->clickLink('Page Manager Test Block');
$edit = [
'region' => 'top',
];
$this
->drupalPostForm(NULL, $edit, 'Add block');
$this
->drupalPostForm(NULL, [], 'Update and save');
$this
->drupalGet('admin/foo');
$this
->assertResponse(200);
$this
->assertText(t('Example output'));
$this
->assertRaw('Page Manager Test Block');
}
protected function doTestExistingPathWithoutParameters() {
$this
->drupalGet('admin');
$this
->assertResponse(200);
$this
->drupalGet('admin/structure/page_manager');
$this
->clickLink('Add page');
$edit = [
'label' => 'existing',
'id' => 'existing',
'path' => 'admin',
'variant_plugin_id' => 'http_status_code',
];
$this
->drupalPostForm(NULL, $edit, 'Next');
$edit = [
'page_variant_label' => 'Status Code',
'variant_settings[status_code]' => 404,
];
$this
->drupalPostForm(NULL, $edit, 'Finish');
$this
->drupalGet('admin');
$this
->assertResponse(404);
}
protected function doTestUpdateSubmit() {
$this
->drupalGet('admin/structure/page_manager/manage/foo/general');
$this
->clickLink('Add variant');
$edit = [
'variant_plugin_id' => 'block_display',
'label' => 'First',
];
$this
->drupalPostForm(NULL, $edit, 'Next');
$edit = [
'variant_settings[page_title]' => 'Example title',
];
$this
->drupalPostForm(NULL, $edit, 'Next');
$this
->drupalPostForm(NULL, [], 'Finish');
$edit = [
'page_variant_label' => 'First updated',
'variant_settings[page_title]' => 'Example title updated',
];
$this
->drupalPostForm('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__general', $edit, 'Update');
$this
->assertFieldByName('page_variant_label', 'First updated');
$this
->assertFieldByName('variant_settings[page_title]', 'Example title updated');
$this
->drupalPostForm('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__contexts', [], 'Update');
$this
->assertUrl('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__contexts');
$this
->drupalPostForm('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__selection', [], 'Update');
$this
->assertUrl('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__selection');
$this
->drupalPostForm('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__content', [], 'Update');
$this
->assertUrl('admin/structure/page_manager/manage/foo/page_variant__foo-block_display-0__content');
}
protected function doTestDeletePage() {
$this
->drupalGet('admin/structure/page_manager');
$this
->clickLink('Delete');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertRaw(new FormattableMarkup('The page %name has been removed.', [
'%name' => 'existing',
]));
$this
->drupalGet('admin');
$this
->assertResponse(200);
$this
->drupalGet('admin/structure/page_manager');
$this
->clickLink('Delete');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertRaw(new FormattableMarkup('The page %name has been removed.', [
'%name' => 'Foo',
]));
$this
->drupalGet('admin/foo');
$this
->assertResponse(404);
}
public function testExistingRoutes() {
$this
->drupalGet('admin/structure/page_manager/add');
$edit = [
'label' => 'Placeholder test 2',
'id' => 'placeholder2',
'path' => '/page-manager-test',
'variant_plugin_id' => 'http_status_code',
];
$this
->drupalPostForm(NULL, $edit, 'Next');
$edit = [
'variant_settings[status_code]' => 418,
];
$this
->drupalPostForm(NULL, $edit, 'Finish');
$this
->drupalGet('page-manager-test');
$this
->assertResponse(418);
$page_string = 'test-page';
$this
->drupalGet('page-manager-test/' . $page_string);
$this
->assertResponse(200);
$this
->drupalGet('admin/structure/page_manager/manage/placeholder2/page_variant__placeholder2-http_status_code-0__general');
$this
->clickLink('Delete this variant');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->drupalPostForm(NULL, [], 'Update and save');
$this
->drupalGet('page-manager-test');
$this
->assertResponse(200);
}
protected function assertTheme($theme_name) {
$url = Url::fromUri('base:core/themes/' . $theme_name . '/logo.svg')
->toString();
$elements = $this
->xpath('//img[contains(@src, :url)]', [
':url' => $url,
]);
$this
->assertEqual(count($elements), 1, new FormattableMarkup('Page is rendered in @theme', [
'@theme' => $theme_name,
]));
}
protected function findBlockByLabel($page_variant_id, $block_label) {
if ($page_variant = PageVariant::load($page_variant_id)) {
$variant_plugin = $page_variant
->getVariantPlugin();
foreach ($variant_plugin
->getRegionAssignments() as $blocks) {
foreach ($blocks as $block) {
if ($block
->label() == $block_label) {
return $block;
}
}
}
}
return NULL;
}
}