/** * Tests whether the page entity config schema is valid. */ public function testValidPageConfigSchema() { $id = 'node_view'; /** @var \Drupal\page_manager\PageInterface $page */ $page = Page::load($id); // Add an access condition. $page->addAccessCondition(['id' => 'node_type', 'bundles' => ['article' => 'article'], 'negate' => TRUE, 'context_mapping' => ['node' => 'node']]); $page->save(); $page_variant_id = 'block_page'; // Add a block variant. $page_variant = PageVariant::create(['variant' => 'block_display', 'id' => $page_variant_id, 'label' => 'Block page', 'page' => $page->id()]); $page_variant->save(); $page->addVariant($page_variant); /** @var \Drupal\page_manager\Plugin\DisplayVariant\PageBlockDisplayVariant $variant_plugin */ $variant_plugin = $page_variant->getVariantPlugin(); // Add a selection condition. $page_variant->addSelectionCondition(['id' => 'node_type', 'bundles' => ['page' => 'page'], 'context_mapping' => ['node' => 'node']]); // Add a block. $variant_plugin->addBlock(['id' => 'entity_view:node', 'label' => 'View the node', 'provider' => 'page_manager', 'label_display' => 'visible', 'view_mode' => 'default']); $page_variant->save(); $page_config = \Drupal::config("page_manager.page.{$id}"); $this->assertSame($page_config->get('id'), $id); $variant_config = \Drupal::config("page_manager.page_variant.{$page_variant_id}"); $this->assertSame($variant_config->get('id'), $page_variant_id); $this->assertConfigSchema(\Drupal::service('config.typed'), $page_config->getName(), $page_config->get()); $this->assertConfigSchema(\Drupal::service('config.typed'), $variant_config->getName(), $variant_config->get()); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); // Remove the 'access content' permission from anonymous and auth users. Role::load(RoleInterface::ANONYMOUS_ID)->revokePermission('access content')->save(); Role::load(RoleInterface::AUTHENTICATED_ID)->revokePermission('access content')->save(); $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']); $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']); $this->drupalPlaceBlock('page_title_block'); $this->page = Page::load('node_view'); }
/** * @covers ::getVariants */ public function testGetVariants() { $variant1 = $this->prophesize(PageVariantInterface::class); $variant1->id()->willReturn('variant1'); $variant1->getWeight()->willReturn(0); $variant2 = $this->prophesize(PageVariantInterface::class); $variant2->id()->willReturn('variant2'); $variant2->getWeight()->willReturn(-10); $entity_storage = $this->prophesize(EntityStorageInterface::class); $entity_storage->loadByProperties(['page' => 'the_page'])->willReturn(['variant1' => $variant1->reveal(), 'variant2' => $variant2->reveal()])->shouldBeCalledTimes(1); $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class); $entity_type_manager->getStorage('page_variant')->willReturn($entity_storage); $container = new ContainerBuilder(); $container->set('entity_type.manager', $entity_type_manager->reveal()); \Drupal::setContainer($container); $page = new Page(['id' => 'the_page'], 'page'); $variants = $page->getVariants(); $this->assertSame(['variant2' => $variant2->reveal(), 'variant1' => $variant1->reveal()], $variants); $variants = $page->getVariants(); $this->assertSame(['variant2' => $variant2->reveal(), 'variant1' => $variant1->reveal()], $variants); }
/** * {@inheritdoc} */ 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_handler')->install(['bartik', 'classy']); $this->config('system.theme')->set('admin', 'classy')->save(); $this->drupalLogin($this->drupalCreateUser(['administer pages', 'access administration pages', 'view the administration theme'])); // Remove the default node_view page to start with a clean UI. Page::load('node_view')->delete(); }
/** * @covers ::getContexts */ public function testGetContexts() { $context = new Context(new ContextDefinition('bar')); $event_dispatcher = $this->prophesize(EventDispatcherInterface::class); $event_dispatcher->dispatch(PageManagerEvents::PAGE_CONTEXT, Argument::type(PageManagerContextEvent::class))->will(function ($args) use($context) { $args[1]->getPage()->addContext('foo', $context); }); $container = new ContainerBuilder(); $container->set('event_dispatcher', $event_dispatcher->reveal()); \Drupal::setContainer($container); $contexts = $this->page->getContexts(); $this->assertSame(['foo' => $context], $contexts); }
/** * Tests whether the page entity config schema is valid. */ public function testValidPageConfigSchema() { $id = strtolower($this->randomMachineName()); /** @var $page \Drupal\page_manager\PageInterface */ $page = Page::create(['id' => $id, 'label' => $this->randomMachineName(), 'path' => '/node/{node}']); // Add an access condition. $page->addAccessCondition(['id' => 'node_type', 'bundles' => ['article' => 'article'], 'negate' => TRUE, 'context_mapping' => ['node' => 'node']]); // Add a block display variant. $display_variant_id = $page->addVariant(['id' => 'block_display', 'label' => 'Block page']); /** @var $display_variant \Drupal\page_manager\Plugin\DisplayVariant\BlockDisplayVariant */ $display_variant = $page->getVariant($display_variant_id); // Add a selection condition. $display_variant->addSelectionCondition(['id' => 'node_type', 'bundles' => ['page' => 'page'], 'context_mapping' => ['node' => 'node']]); // Add a block. $display_variant->addBlock(['id' => 'entity_view:node', 'label' => 'View the node', 'provider' => 'page_manager', 'label_display' => 'visible', 'view_mode' => 'default']); $page->save(); $config = \Drupal::config("page_manager.page.{$id}"); $this->assertEqual($config->get('id'), $id); $this->assertConfigSchema(\Drupal::service('config.typed'), $config->getName(), $config->get()); }
/** * Tests that a node bundle condition controls the node view page. */ public function testPagePlaceHolder() { // Access the page callback and check whether string is printed. $page_string = 'test-page'; $this->drupalGet('page-manager-test/' . $page_string); $this->assertResponse(200); $this->assertCacheTag('page_manager_route_name:page_manager_test.page_view'); $this->assertText('Hello World! Page ' . $page_string); // Create a new page entity with the same path as in the test module. $page = Page::create(['label' => 'Placeholder test', 'id' => 'placeholder', 'path' => '/page-manager-test/%']); $page->save(); // Create a new variant. $http_status_variant = PageVariant::create(['variant' => 'http_status_code', 'label' => 'HTTP status code', 'id' => 'http_status_code', 'page' => 'placeholder']); $http_status_variant->getVariantPlugin()->setConfiguration(['status_code' => 200]); $http_status_variant->save(); $this->triggerRouterRebuild(); // Access the page callback again and check that now the text is not there. $this->drupalGet('page-manager-test/' . $page_string); $this->assertResponse(200); $this->assertCacheTag('page_manager_route_name:page_manager_test.page_view'); $this->assertNoText('Hello World! Page ' . $page_string); }
/** * @covers ::filterParameters */ public function testFilterParameters() { $parameters = ['foo' => ['machine_name' => 'foo', 'type' => 'integer', 'label' => 'Foo'], 'bar' => ['machine_name' => 'bar', 'type' => '', 'label' => '']]; $page = new Page(['id' => 'the_page', 'parameters' => $parameters], 'page'); $expected = $parameters; $this->assertEquals($expected, $page->getParameters()); $method = new \ReflectionMethod($page, 'filterParameters'); $method->setAccessible(TRUE); $method->invoke($page); $expected = ['foo' => ['machine_name' => 'foo', 'type' => 'integer', 'label' => 'Foo']]; $this->assertEquals($expected, $page->getParameters()); }
/** * Gets the page this variant is on. * * @return \Drupal\page_manager\Entity\Page */ protected function getPage() { if (!$this->page) { throw new \UnexpectedValueException('The page variant has no associated page'); } return Page::load($this->page); }
/** * Tests that a node bundle condition controls the node view page. */ public function testAdmin() { // Create two nodes, and view their pages. $node1 = $this->drupalCreateNode(['type' => 'page']); $node2 = $this->drupalCreateNode(['type' => 'article']); $node3 = $this->drupalCreateNode(['type' => 'article']); $this->drupalGet('node/' . $node1->id()); $this->assertResponse(200); $this->assertText($node1->label()); $this->assertTitle($node1->label() . ' | Drupal'); $this->drupalGet('node/' . $node2->id()); $this->assertResponse(200); $this->assertCacheTag('page_manager_route_name:entity.node.canonical'); $this->assertText($node2->label()); $this->assertTitle($node2->label() . ' | Drupal'); // Create a new page entity to take over node pages. $edit = ['label' => 'Node View', 'id' => 'node_view', 'path' => 'node/%']; $this->drupalPostForm('admin/structure/page_manager/add', $edit, 'Save'); // Create a new variant to always return 404. $edit = ['id' => 'http_status_code', 'variant_settings[status_code]' => 404]; $this->drupalPostForm('admin/structure/page_manager/manage/node_view/add/http_status_code', $edit, 'Save'); $this->drupalGet('node/' . $node1->id()); $this->assertResponse(404); $this->assertCacheTag('page_manager_route_name:entity.node.canonical'); $this->assertNoText($node1->label()); $this->drupalGet('node/' . $node2->id()); $this->assertResponse(404); $this->assertNoText($node2->label()); // Add a new variant. $this->drupalGet('admin/structure/page_manager/manage/node_view'); $this->clickLink('Add new variant'); $this->clickLink('Block page'); $edit = ['id' => 'block_page_first', 'label' => 'First']; $this->drupalPostForm(NULL, $edit, 'Save'); // Add the entity view block. $this->clickLink('Add new block'); $this->clickLink('Entity view (Content)'); $edit = ['region' => 'top', 'settings[label_display]' => FALSE]; $this->drupalPostForm(NULL, $edit, 'Add block'); // Add a node bundle condition for articles. $this->clickLink('Add new selection condition'); $this->clickLink('Node Bundle'); $edit = ['condition[bundles][article]' => TRUE]; $this->drupalPostForm(NULL, $edit, 'Add selection condition'); // Set the page title to the node title. $edit = ['variant_settings[page_title]' => '[node:title]']; $this->drupalPostForm(NULL, $edit, 'Save'); /** @var \Drupal\page_manager\PageInterface $page */ $page = Page::load('node_view'); foreach ($page->getVariants() as $block_variant_uuid => $block_variant) { if ($block_variant->label() == 'First') { break; } } // Set the weight of the block_page variant to -10. $edit = ['variants[' . $block_variant_uuid . '][weight]' => -10]; $this->drupalPostForm(NULL, $edit, 'Save'); // The page node will 404, but the article node will display the variant. $this->drupalGet('node/' . $node1->id()); $this->assertResponse(404); $this->assertNoText($node1->label()); $this->drupalGet('node/' . $node2->id()); $this->assertResponse(200); $this->assertTitle($node2->label() . ' | Drupal'); $this->assertText($node2->body->value); // Test cacheability metadata. $this->drupalGet('node/' . $node3->id()); $this->assertNoText($node2->label()); // Ensure this doesn't affect the /node/add page. $this->drupalGet('node/add'); $this->assertResponse(200); }
/** * Finds a display variant based on its page and display variant label. * * @param string $page_id * The ID of the page entity. * @param string $display_variant_label * The label of the display variant. * * @return \Drupal\Core\Display\VariantInterface|null * Either a display variant, or NULL. */ protected function findDisplayVariantByLabel($page_id, $display_variant_label) { if ($page = Page::load($page_id)) { /** @var $page \Drupal\page_manager\PageInterface */ foreach ($page->getVariants() as $display_variant) { if ($display_variant->label() == $display_variant_label) { return $display_variant; } } } return NULL; }
/** * Tests the parameters UI. */ public function testParameters() { $this->drupalGet('admin/structure/page_manager'); $this->clickLink('Add page'); $edit = ['id' => 'foo', 'label' => 'Foo', 'path' => '/foo/{user}']; $this->drupalPostForm(NULL, $edit, 'Save'); $this->assertText('No context assigned'); $this->clickLink('Edit'); $this->drupalPostForm(NULL, ['type' => 'entity:user'], 'Update parameter'); $this->assertNoText('No context assigned'); $this->assertText('entity:user'); $parameters = Page::load('foo')->getParameters(); $this->assertIdentical(['user' => ['machine_name' => 'user', 'type' => 'entity:user', 'label' => 'User']], $parameters); }