2015-08-17 17:00:26 -07:00
< ? php
namespace Drupal\help\Tests ;
use Drupal\simpletest\WebTestBase ;
/**
* Verify help display and user access to help based on permissions .
*
* @ group help
*/
class HelpTest extends WebTestBase {
/**
* Modules to enable .
*
* The help_test module implements hook_help () but does not provide a module
2016-04-20 09:56:34 -07:00
* overview page . The help_page_test module has a page section plugin that
* returns no links .
2015-08-17 17:00:26 -07:00
*
* @ var array .
*/
2016-04-20 09:56:34 -07:00
public static $modules = array ( 'help_test' , 'help_page_test' );
2015-08-17 17:00:26 -07:00
/**
* Use the Standard profile to test help implementations of many core modules .
*/
protected $profile = 'standard' ;
/**
* The admin user that will be created .
*/
protected $adminUser ;
/**
* The anonymous user that will be created .
*/
protected $anyUser ;
protected function setUp () {
parent :: setUp ();
$this -> getModuleList ();
// Create users.
$this -> adminUser = $this -> drupalCreateUser ( array ( 'access administration pages' , 'view the administration theme' , 'administer permissions' ));
$this -> anyUser = $this -> drupalCreateUser ( array ());
}
/**
2016-04-20 09:56:34 -07:00
* Logs in users , tests help pages .
2015-08-17 17:00:26 -07:00
*/
public function testHelp () {
// Login the root user to ensure as many admin links appear as possible on
// the module overview pages.
$this -> drupalLogin ( $this -> rootUser );
$this -> verifyHelp ();
// Login the regular user.
$this -> drupalLogin ( $this -> anyUser );
$this -> verifyHelp ( 403 );
// Verify that introductory help text exists, goes for 100% module coverage.
$this -> drupalLogin ( $this -> adminUser );
$this -> drupalGet ( 'admin/help' );
2016-04-20 09:56:34 -07:00
$this -> assertRaw ( t ( 'For more information, refer to the help listed on this page or to the <a href=":docs">online documentation</a> and <a href=":support">support</a> pages at <a href=":drupal">drupal.org</a>.' , array ( ':docs' => 'https://www.drupal.org/documentation' , ':support' => 'https://www.drupal.org/support' , ':drupal' => 'https://www.drupal.org' )));
// Verify that hook_help() section title and description appear.
$this -> assertRaw ( '<h2>' . t ( 'Module overviews' ) . '</h2>' );
$this -> assertRaw ( '<p>' . t ( 'Module overviews are provided by modules. Overviews available for your installed modules:' ), '</p>' );
2015-08-17 17:00:26 -07:00
2016-04-20 09:56:34 -07:00
// Verify that an empty section is handled correctly.
$this -> assertRaw ( '<h2>' . t ( 'Empty section' ) . '</h2>' );
$this -> assertRaw ( '<p>' . t ( 'This description should appear.' ), '</p>' );
$this -> assertText ( t ( 'There is currently nothing in this section.' ));
2015-08-17 17:00:26 -07:00
// Make sure links are properly added for modules implementing hook_help().
foreach ( $this -> getModuleList () as $module => $name ) {
$this -> assertLink ( $name , 0 , format_string ( 'Link properly added to @name (admin/help/@module)' , array ( '@module' => $module , '@name' => $name )));
}
// Ensure that module which does not provide an module overview page is
// handled correctly.
$this -> clickLink ( \Drupal :: moduleHandler () -> getName ( 'help_test' ));
$this -> assertRaw ( t ( 'No help is available for module %module.' , array ( '%module' => \Drupal :: moduleHandler () -> getName ( 'help_test' ))));
2016-04-20 09:56:34 -07:00
// Verify that the order of topics is alphabetical by displayed module
// name, by checking the order of some modules, including some that would
// have a different order if it was done by machine name instead.
$this -> drupalGet ( 'admin/help' );
$page_text = $this -> getTextContent ();
$start = strpos ( $page_text , 'Module overviews' );
$pos = $start ;
$list = [ 'Block' , 'Color' , 'Custom Block' , 'History' , 'Text Editor' ];
foreach ( $list as $name ) {
$this -> assertLink ( $name );
$new_pos = strpos ( $page_text , $name , $start );
$this -> assertTrue ( $new_pos > $pos , 'Order of ' . $name . ' is correct on page' );
$pos = $new_pos ;
}
2015-08-17 17:00:26 -07:00
}
/**
2016-04-20 09:56:34 -07:00
* Verifies the logged in user has access to the various help pages .
2015-08-17 17:00:26 -07:00
*
2016-01-06 16:31:26 -08:00
* @ param int $response
* ( optional ) An HTTP response code . Defaults to 200.
2015-08-17 17:00:26 -07:00
*/
protected function verifyHelp ( $response = 200 ) {
$this -> drupalGet ( 'admin/index' );
$this -> assertResponse ( $response );
if ( $response == 200 ) {
$this -> assertText ( 'This page shows you all available administration tasks for each module.' );
}
else {
$this -> assertNoText ( 'This page shows you all available administration tasks for each module.' );
}
foreach ( $this -> getModuleList () as $module => $name ) {
2016-04-20 09:56:34 -07:00
// View module help page.
2015-08-17 17:00:26 -07:00
$this -> drupalGet ( 'admin/help/' . $module );
$this -> assertResponse ( $response );
if ( $response == 200 ) {
$this -> assertTitle ( $name . ' | Drupal' , format_string ( '%module title was displayed' , array ( '%module' => $module )));
2015-10-08 11:40:12 -07:00
$this -> assertEqual ( $this -> cssSelect ( 'h1.page-title' )[ 0 ], t ( $name ), format_string ( '%module heading was displayed' , array ( '%module' => $module )));
2015-08-17 17:00:26 -07:00
$admin_tasks = system_get_module_admin_tasks ( $module , system_get_info ( 'module' , $module ));
if ( ! empty ( $admin_tasks )) {
$this -> assertText ( t ( '@module administration pages' , array ( '@module' => $name )));
}
foreach ( $admin_tasks as $task ) {
$this -> assertLink ( $task [ 'title' ]);
2015-10-08 11:40:12 -07:00
// Ensure there are no double escaped '&' or '<' characters.
$this -> assertNoEscaped ( '&' , 'The help text does not have double escaped &.' );
$this -> assertNoEscaped ( '<' , 'The help text does not have double escaped <.' );
// Ensure there are no escaped '<' characters.
$this -> assertNoEscaped ( '<' , 'The help text does not have single escaped <.' );
2015-08-17 17:00:26 -07:00
}
2015-10-08 11:40:12 -07:00
// Ensure there are no double escaped '&' or '<' characters.
$this -> assertNoEscaped ( '&' );
$this -> assertNoEscaped ( '<' );
// Ensure there are no escaped '<' characters.
$this -> assertNoEscaped ( '<' );
2015-08-17 17:00:26 -07:00
}
}
}
/**
* Gets the list of enabled modules that implement hook_help () .
*
* @ return array
* A list of enabled modules .
*/
protected function getModuleList () {
$modules = array ();
$module_data = system_rebuild_module_data ();
foreach ( \Drupal :: moduleHandler () -> getImplementations ( 'help' ) as $module ) {
$modules [ $module ] = $module_data [ $module ] -> info [ 'name' ];
}
return $modules ;
}
}