X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fviews%2Ftests%2Fsrc%2FFunctional%2FHandler%2FAreaTest.php;fp=web%2Fcore%2Fmodules%2Fviews%2Ftests%2Fsrc%2FFunctional%2FHandler%2FAreaTest.php;h=d631373d23ef00921e9823ad6e7d147c2a477e2e;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/views/tests/src/Functional/Handler/AreaTest.php b/web/core/modules/views/tests/src/Functional/Handler/AreaTest.php new file mode 100644 index 000000000..d631373d2 --- /dev/null +++ b/web/core/modules/views/tests/src/Functional/Handler/AreaTest.php @@ -0,0 +1,217 @@ +enableViewsTestModule(); + } + + protected function viewsData() { + $data = parent::viewsData(); + $data['views']['test_example'] = [ + 'title' => 'Test Example area', + 'help' => 'A area handler which just exists for tests.', + 'area' => [ + 'id' => 'test_example' + ] + ]; + + return $data; + } + + + /** + * Tests the generic UI of a area handler. + */ + public function testUI() { + $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']); + $this->drupalLogin($admin_user); + + $types = ['header', 'footer', 'empty']; + $labels = []; + foreach ($types as $type) { + $edit_path = 'admin/structure/views/nojs/handler/test_example_area/default/' . $type . '/test_example'; + + // First setup an empty label. + $this->drupalPostForm($edit_path, [], t('Apply')); + $this->assertText('Test Example area'); + + // Then setup a no empty label. + $labels[$type] = $this->randomMachineName(); + $this->drupalPostForm($edit_path, ['options[admin_label]' => $labels[$type]], t('Apply')); + // Make sure that the new label appears on the site. + $this->assertText($labels[$type]); + + // Test that the settings (empty/admin_label) are accessible. + $this->drupalGet($edit_path); + $this->assertField('options[admin_label]'); + if ($type !== 'empty') { + $this->assertField('options[empty]'); + } + } + } + + /** + * Tests the rendering of an area. + */ + public function testRenderArea() { + $view = Views::getView('test_example_area'); + $view->initHandlers(); + + // Insert a random string with XSS injection in the test area plugin. + // Ensure that the string is rendered for the header, footer, and empty + // text with the markup properly escaped. + $header_string = '

' . $this->randomMachineName() . '

'; + $footer_string = '

' . $this->randomMachineName() . '

'; + $empty_string = '

' . $this->randomMachineName() . '

'; + + $view->header['test_example']->options['string'] = $header_string; + $view->header['test_example']->options['empty'] = TRUE; + + $view->footer['test_example']->options['string'] = $footer_string; + $view->footer['test_example']->options['empty'] = TRUE; + + $view->empty['test_example']->options['string'] = $empty_string; + + // Check whether the strings exist in the output and are sanitized. + $output = $view->preview(); + $output = $this->container->get('renderer')->renderRoot($output); + $this->assertTrue(strpos($output, Xss::filterAdmin($header_string)) !== FALSE, 'Views header exists in the output and is sanitized'); + $this->assertTrue(strpos($output, Xss::filterAdmin($footer_string)) !== FALSE, 'Views footer exists in the output and is sanitized'); + $this->assertTrue(strpos($output, Xss::filterAdmin($empty_string)) !== FALSE, 'Views empty exists in the output and is sanitized'); + $this->assertTrue(strpos($output, 'initDisplay(); + $view->initHandlers(); + $handlers = $view->display_handler->getHandlers('empty'); + $this->assertEqual(0, count($handlers)); + + $output = $view->preview(); + $output = \Drupal::service('renderer')->renderRoot($output); + // The area output should not be present since access was denied. + $this->assertFalse(strpos($output, 'a custom string') !== FALSE); + $view->destroy(); + + // Test with access granted for the area handler. + $view = Views::getView('test_example_area_access'); + $view->initDisplay(); + $view->display_handler->overrideOption('empty', [ + 'test_example' => [ + 'field' => 'test_example', + 'id' => 'test_example', + 'table' => 'views', + 'plugin_id' => 'test_example', + 'string' => 'a custom string', + 'custom_access' => TRUE, + ], + ]); + $view->initHandlers(); + $handlers = $view->display_handler->getHandlers('empty'); + + $output = $view->preview(); + $output = \Drupal::service('renderer')->renderRoot($output); + $this->assertTrue(strpos($output, 'a custom string') !== FALSE); + $this->assertEqual(1, count($handlers)); + } + + /** + * Tests global tokens. + */ + public function testRenderAreaToken() { + $admin_user = $this->drupalCreateUser(['administer views', 'administer site configuration']); + $this->drupalLogin($admin_user); + + $view = Views::getView('test_example_area'); + $view->initHandlers(); + + $this->drupalGet('admin/structure/views/nojs/handler/test_example_area/default/empty/test_example'); + + // Test that the list is token present. + $element = $this->xpath('//ul[@class="global-tokens"]'); + $this->assertTrue($element, 'Token list found on the options form.'); + + $empty_handler = &$view->empty['test_example']; + + // Test the list of available tokens. + $available = $empty_handler->getAvailableGlobalTokens(); + foreach (['site', 'view'] as $type) { + $this->assertTrue(!empty($available[$type]) && is_array($available[$type])); + // Test that each item exists in the list. + foreach ($available[$type] as $token => $info) { + $this->assertText("[$type:$token]"); + } + } + + // Test the rendered output of a token. + $empty_handler->options['string'] = '[site:name]'; + + // Test we have the site:name token in the output. + $output = $view->preview(); + $output = $this->container->get('renderer')->renderRoot($output); + $expected = \Drupal::token()->replace('[site:name]'); + $this->assertTrue(strpos($output, $expected) !== FALSE); + } + + /** + * Tests overriding the view title using the area title handler. + */ + public function testTitleArea() { + $view = Views::getView('frontpage'); + $view->initDisplay('page_1'); + + // Add the title area handler to the empty area. + $view->displayHandlers->get('page_1')->overrideOption('empty', [ + 'title' => [ + 'id' => 'title', + 'table' => 'views', + 'field' => 'title', + 'admin_label' => '', + 'empty' => '0', + 'title' => 'Overridden title', + 'plugin_id' => 'title', + ], + ]); + + $view->storage->enable()->save(); + + $this->drupalGet('node'); + $this->assertText('Overridden title', 'Overridden title found.'); + } + +}