You are here

SimpleTestExampleMockModuleTest.php in Examples for Developers 8

File

testing_example/src/Tests/SimpleTestExampleMockModuleTest.php
View source
<?php

namespace Drupal\testing_example\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Demonstrate SimpleTest with a mock module.
 *
 * SimpleTestExampleMockModuleTestCase allows us to demonstrate how you can
 * use a mock module to aid in functional testing in Drupal.
 *
 * If you have some functionality that's not intrinsic to the code under test,
 * you can add a special mock module that only gets installed during test
 * time. This allows you to implement APIs created by your module, or otherwise
 * exercise the code in question.
 *
 * This test case class is very similar to SimpleTestExampleTestCase. The main
 * difference is that we enable the testing_example_test module by providing
 * it in the $modules property. Then we can test for behaviors provided by that
 * module.
 *
 * @see SimpleTestExampleTestCase
 *
 * @ingroup testing_example
 *
 * SimpleTest uses group annotations to help you organize your tests.
 *
 * @group testing_example
 * @group examples
 */
class SimpleTestExampleMockModuleTest extends WebTestBase {

  /**
   * Our module dependencies.
   *
   * In Drupal 8's SimpleTest, we declare module dependencies in a public
   * static property called $modules.
   *
   * @var array
   */
  public static $modules = [
    'testing_example',
    'testing_example_test',
  ];

  /**
   * Test modifications made by our mock module.
   *
   * We create a simpletest_example node and then see if our submodule
   * operated on it.
   */
  public function testSimpleTestExampleMockModule() {

    // Create a user.
    $test_user = $this
      ->drupalCreateUser([
      'access content',
    ]);

    // Log them in.
    $this
      ->drupalLogin($test_user);

    // Set up some content.
    $settings = [
      'type' => 'testing_example',
      'title' => $this
        ->randomMachineName(32),
    ];

    // Create the content node.
    $node = $this
      ->drupalCreateNode($settings);

    // View the node.
    $this
      ->drupalGet('node/' . $node
      ->id());

    // Check that our module did it's thing.
    $this
      ->assertText('The test module did its thing.', 'Found evidence of test module.');
  }

}

Classes

Namesort descending Description
SimpleTestExampleMockModuleTest Demonstrate SimpleTest with a mock module.