Usage

Drupal API driver

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php

use Drupal\Driver\DrupalDriver;
use Drupal\Driver\Cores\Drupal8;

require 'vendor/autoload.php';

// Path to Drupal.
$path = './drupal-8';

// Host.
$uri = 'http://d8.devl';

$driver = new DrupalDriver($path, $uri);
$driver->setCoreFromVersion();

// Bootstrap Drupal.
$driver->bootstrap();

// Create a node.
$node = (object) array(
  'type' => 'article',
  'uid' => 1,
  'title' => $driver->getRandom()->name(),
);
$driver->createNode($node);

Drush driver

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

use Drupal\Driver\DrushDriver;

...

$alias = '@mysite';
$driver = new DrushDriver($alias);

...

Blackbox

Note, the blackbox driver has no ability to control Drupal, and is provided as a fallback for when some tests can run without such access.

Any testing application should catch unsupported driver exceptions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

use Drupal\Driver\BlackboxDriver;
use Drupal\Driver\Exception\UnsupportedDriverActionException;

...

$driver = new BlackboxDriver($alias);

try {
  // Create a node.
  $node = (object) array(
    'type' => 'article',
    'uid' => 1,
    'title' => $driver->getRandom()->name(),
  );
  $driver->createNode($node);
}
catch (UnsupportedDriverActionException $e) {
  // Mark test as skipped.
}

Practical example with PHPUnit

By using the phpunit/mink project in conjunction with the Drupal Driver, one can use PHPUnit to drive browser sessions and control Drupal.

To install:

1
2
3
4
5
6
{
    "require": {
        "aik099/phpunit-mink": "~2.0",
        "drupal/drupal-driver": "~1.0"
    }
}

and then, in the tests directory, a sample test:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

use aik099\PHPUnit\BrowserTestCase;

use Drupal\Driver\DrupalDriver;

class GeneralTest extends BrowserTestCase
{

    /**
     * @var \Drupal\Driver\DriverInterface
     */
    protected $driver;

    // Path to a Drupal install. This example assumes the directory is in the same one as the `composer.json` file.
    protected static $drupalRoot = './drupal';

    // Url to the homepage of the Drupal install.
    protected static $uri = 'http://d8.devl';

    public static $browsers = array(
        // Selenium info.
        array(
            'host' => 'localhost',
            'port' => 4444,
            'browserName' => 'firefox',
            'baseUrl' => 'http://d8.devl',
        ),
    );

    public function setUp() {
        $this->driver = new DrupalDriver(static::$drupalRoot, static::$uri);
        $this->driver->setCoreFromVersion();
        $this->driver->bootstrap();
    }

    public function testUsingSession()
    {
        // This is Mink's Session.
        $session = $this->getSession();

        // Go to a page.
        $session->visit(static::$uri);

        // Validate text presence on a page.
        $this->assertTrue($session->getPage()->hasContent('Site-Install'));
    }

    public function testUsingBrowser()
    {
        // Prints the name of used browser.
        echo sprintf(
            "I'm executed using '%s' browser",
            $this->getBrowser()->getBrowserName()
        );
    }

    public function testNodeCreate() {
        $node = (object) [
            'title' => $this->driver->getRandom()->string(),
            'type' => 'article',
        ];
        $this->driver->createNode($node);

        $session = $this->getSession();
        $session->visit(static::$uri . '/node/' . $node->nid);

        $this->assertTrue($session->getPage()->hasContent($node->title));
    }

}