function HooksExampleTest::testHooksExample

Same name and namespace in other branches
  1. 4.0.x modules/hooks_example/tests/src/Functional/HooksExampleTest.php \Drupal\Tests\hooks_example\Functional\HooksExampleTest::testHooksExample()

Test the output of the example page.

File

modules/hooks_example/tests/src/Functional/HooksExampleTest.php, line 47

Class

HooksExampleTest
Test the functionality of the Hooks Example module.

Namespace

Drupal\Tests\hooks_example\Functional

Code

public function testHooksExample() {
    // Make sure our menus and links work.
    $this->drupalGet('<front>');
    $this->assertSession()
        ->linkExists('Hooks Example');
    // Test the description page at examples/hook-example returns a 200.
    $this->drupalGet('examples/hooks-example');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Test that our implementation of hook_help() works.
    $this->drupalGet('admin/help/hooks_example');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains('This text is provided by the function hooks_example_help()');
    // Test that our implementation of hook_node_view() works.
    // Create a new node.
    $settings = [
        'type' => 'page',
        'title' => 'Hooks Example Testing Node',
        'status' => 1,
    ];
    $node = $this->drupalCreateNode($settings);
    $this->drupalGet($node->toUrl());
    // Test that the output added to the page by hooks_example_node_view() is
    // present. Which also tests that our page view counting was initialized.
    $this->assertSession()
        ->pageTextContains('You have viewed this node 1 times this session.');
    // Tests that the message set by
    // hooks_example_hooks_example_count_incremented() is displayed on the page.
    // Which also has the effect of testing to see wehther or not our custom
    // hook is being invoked.
    $this->assertSession()
        ->pageTextContains('This is the first time you have viewed the node ' . $node->label() . '.');
    // Navigate to a new page, and then back and verify the counter was updated.
    $this->drupalGet('<front>');
    $this->drupalGet($node->toUrl());
    $this->assertSession()
        ->pageTextContains('You have viewed this node 2 times this session.');
    $this->assertSession()
        ->pageTextNotContains('This is the first time you have viewed the node ' . $node->label() . '.');
    // Test our implementation of hook_form_alter().
    $this->drupalLogout();
    $this->drupalGet('user/login');
    $this->assertSession()
        ->pageTextContains('This text has been altered by hooks_example_form_alter().');
}