class SimpleTestExampleMockModuleTest

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.

@group testing_example @group examples

Hierarchy

Expanded class hierarchy of SimpleTestExampleMockModuleTest

See also

SimpleTestExampleTestCase

SimpleTest uses group annotations to help you organize your tests.

Related topics

File

testing_example/src/Tests/SimpleTestExampleMockModuleTest.php, line 32

Namespace

Drupal\testing_example\Tests
View source
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.');
    }

}

Members

Title Sort descending Modifiers Object type Summary
SimpleTestExampleMockModuleTest::$modules public static property Our module dependencies.
SimpleTestExampleMockModuleTest::testSimpleTestExampleMockModule public function Test modifications made by our mock module.