Skip to content

Commit fbbe458

Browse files
committed
[#532] Added support for Functional JS tests.
1 parent 050e0c5 commit fbbe458

117 files changed

Lines changed: 1361 additions & 109 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.ahoy.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ commands:
274274
test-functional:
275275
usage: Run PHPUnit functional tests.
276276
cmd: ahoy cli vendor/bin/phpunit --testsuite=functional "$@"
277+
278+
test-functional-javascript:
279+
aliases: [test-js]
280+
usage: Run PHPUnit functional JavaScript tests.
281+
cmd: ahoy cli vendor/bin/phpunit --testsuite=functional-javascript "$@"
277282
#;> TOOL_PHPUNIT
278283

279284
#;< TOOL_BEHAT

.docker/cli.dockerfile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ ARG VORTEX_FRONTEND_BUILD_SKIP="0"
3939
ENV VORTEX_FRONTEND_BUILD_SKIP=${VORTEX_FRONTEND_BUILD_SKIP}
4040

4141
ENV COMPOSER_ALLOW_SUPERUSER=1 \
42-
COMPOSER_CACHE_DIR=/tmp/.composer/cache \
43-
SIMPLETEST_DB=mysql://drupal:drupal@database/drupal \
44-
SIMPLETEST_BASE_URL=http://nginx:8080 \
45-
SYMFONY_DEPRECATIONS_HELPER=disabled
42+
COMPOSER_CACHE_DIR=/tmp/.composer/cache
4643

4744
# Allow custom PHP runtime configuration for Drush CLI commands.
4845
# The leading colon appends to the default scan directories.

.vortex/.ahoy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,14 @@ commands:
117117
118118
# If there are changes to the snapshots - this command will re-run twice reporting error the first time.
119119
update-snapshots:
120+
aliases: [us]
120121
cmd: |
121122
export XDEBUG_MODE=off
122123
composer --working-dir=tests update-snapshots
123124
composer --working-dir=installer update-snapshots
124125
125126
update-snapshots-install:
127+
aliases: [usi]
126128
cmd: |
127129
ahoy install
128130
ahoy update-snapshots

.vortex/installer/tests/Fixtures/handler_process/_baseline/.ahoy.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ commands:
235235
usage: Run PHPUnit functional tests.
236236
cmd: ahoy cli vendor/bin/phpunit --testsuite=functional "$@"
237237

238+
test-functional-javascript:
239+
aliases: [test-js]
240+
usage: Run PHPUnit functional JavaScript tests.
241+
cmd: ahoy cli vendor/bin/phpunit --testsuite=functional-javascript "$@"
242+
238243
test-bdd:
239244
usage: Run BDD tests.
240245
aliases: [test-behat]

.vortex/installer/tests/Fixtures/handler_process/_baseline/.docker/cli.dockerfile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ ARG VORTEX_FRONTEND_BUILD_SKIP="0"
3939
ENV VORTEX_FRONTEND_BUILD_SKIP=${VORTEX_FRONTEND_BUILD_SKIP}
4040

4141
ENV COMPOSER_ALLOW_SUPERUSER=1 \
42-
COMPOSER_CACHE_DIR=/tmp/.composer/cache \
43-
SIMPLETEST_DB=mysql://drupal:drupal@database/drupal \
44-
SIMPLETEST_BASE_URL=http://nginx:8080 \
45-
SYMFONY_DEPRECATIONS_HELPER=disabled
42+
COMPOSER_CACHE_DIR=/tmp/.composer/cache
4643

4744
# Allow custom PHP runtime configuration for Drush CLI commands.
4845
# The leading colon appends to the default scan directories.

.vortex/installer/tests/Fixtures/handler_process/_baseline/docs/testing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class MyClassTest extends UnitTestCase {
5151
- **Unit** - `Drupal\Tests\UnitTestCase` - Testing isolated PHP classes
5252
- **Kernel** - `Drupal\KernelTests\KernelTestBase` - Testing with database/services
5353
- **Functional** - `Drupal\Tests\BrowserTestBase` - Testing with full browser
54+
- **FunctionalJavascript** - `Drupal\FunctionalJavascriptTests\WebDriverTestBase` - Testing browser interaction with JavaScript
5455

5556
### Test data conventions
5657

.vortex/installer/tests/Fixtures/handler_process/_baseline/phpunit.xml

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
beStrictAboutChangesToGlobalState="true"
1010
beStrictAboutOutputDuringTests="true"
1111
beStrictAboutTestsThatDoNotTestAnything="true"
12-
bootstrap="web/core/tests/bootstrap.php"
12+
bootstrap="tests/phpunit/bootstrap.php"
1313
cacheResult="false"
1414
colors="true"
1515
displayDetailsOnPhpunitDeprecations="true"
@@ -31,38 +31,37 @@
3131
<ini name="error_reporting" value="32767"/>
3232
<!-- Do not limit the amount of memory tests take to run. -->
3333
<ini name="memory_limit" value="-1"/>
34-
<!-- Example SIMPLETEST_BASE_URL value: http://localhost -->
35-
<env name="SIMPLETEST_BASE_URL" value=""/>
36-
<!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/database_name#table_prefix -->
37-
<env name="SIMPLETEST_DB" value=""/>
34+
<env name="SIMPLETEST_BASE_URL" value="http://nginx:8080"/>
35+
<env name="SIMPLETEST_DB" value="mysql://drupal:drupal@database/drupal"/>
3836
<!-- By default, browser tests will output links that use the base URL set
3937
in SIMPLETEST_BASE_URL. However, if your SIMPLETEST_BASE_URL is an internal
4038
path (such as may be the case in a virtual or Docker-based environment),
4139
you can set the base URL used in the browser test output links to something
4240
reachable from your host machine here. This will allow you to follow them
4341
directly and view the output. -->
44-
<env name="BROWSERTEST_OUTPUT_BASE_URL" value=""/>
42+
<!-- This is populated dynamically in tests/phpunit/bootstrap.php -->
43+
<!-- <env name="BROWSERTEST_OUTPUT_BASE_URL" value=""/> -->
4544
<!-- The environment variable SYMFONY_DEPRECATIONS_HELPER is used to configure
46-
the behavior of the deprecation tests.
47-
Drupal core's testing framework is setting this variable to its defaults.
48-
Projects with their own requirements need to manage this variable
49-
explicitly.
45+
the behavior of the deprecation tests.
46+
Drupal core's testing framework is setting this variable to its defaults.
47+
Projects with their own requirements need to manage this variable
48+
explicitly.
5049
-->
5150
<!-- To disable deprecation testing completely uncomment the next line. -->
5251
<!-- <env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/> -->
5352
<!-- Deprecation errors can be selectively ignored by specifying a file of
54-
regular expression patterns for exclusion.
55-
Uncomment the line below to specify a custom deprecations ignore file.
56-
NOTE: it may be required to specify the full path to the file to run tests
57-
correctly.
53+
regular expression patterns for exclusion.
54+
Uncomment the line below to specify a custom deprecations ignore file.
55+
NOTE: it may be required to specify the full path to the file to run tests
56+
correctly.
5857
-->
5958
<!-- <env name="SYMFONY_DEPRECATIONS_HELPER" value="ignoreFile=.deprecation-ignore.txt"/> -->
6059
<!-- Example for changing the driver class for mink tests MINK_DRIVER_CLASS value: 'Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver' -->
6160
<env name="MINK_DRIVER_CLASS" value=""/>
6261
<!-- Example for changing the driver args to mink tests MINK_DRIVER_ARGS value: '["http://127.0.0.1:8510"]' -->
6362
<env name="MINK_DRIVER_ARGS" value=""/>
64-
<!-- Example for changing the driver args to webdriver tests MINK_DRIVER_ARGS_WEBDRIVER value: '["chrome", { "goog:chromeOptions": { "w3c": false } }, "http://localhost:4444/wd/hub"]' For using the Firefox browser, replace "chrome" with "firefox" -->
65-
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value=""/>
63+
<!-- Driver args for webdriver tests. -->
64+
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value='["chrome", {"goog:chromeOptions": {"w3c": true, "args": ["--disable-extensions", "--disable-gpu", "--disable-infobars", "--disable-popup-blocking", "--disable-translate", "--force-prefers-reduced-motion", "--no-first-run", "--test-type"]}}, "http://chrome:4444/wd/hub"]'/>
6665
</php>
6766
<testsuites>
6867
<testsuite name="unit">
@@ -79,13 +78,10 @@
7978
<directory>web/themes/custom/**/tests/src/Functional</directory>
8079
</testsuite>
8180

82-
<!-- Not implemented. -->
83-
<!-- See https://github.com/drevops/vortex/issues/820 -->
84-
<!--
8581
<testsuite name="functional-javascript">
86-
<file>./tests/TestSuites/FunctionalJavascriptTestSuite.php</file>
82+
<directory>web/modules/custom/**/tests/src/FunctionalJavascript</directory>
83+
<directory>web/themes/custom/**/tests/src/FunctionalJavascript</directory>
8784
</testsuite>
88-
-->
8985
</testsuites>
9086

9187
<logging>
@@ -124,4 +120,14 @@
124120
<group>skipped</group>
125121
</exclude>
126122
</groups>
123+
<extensions>
124+
<!-- Functional tests HTML output logging.
125+
Output directory is hardcoded in Drupal core's BrowserHtmlDebugTrait
126+
to DRUPAL_ROOT/sites/simpletest/browser_output.
127+
@see https://www.drupal.org/project/drupal/issues/2992069 -->
128+
<bootstrap class="Drupal\TestTools\Extension\HtmlLogging\HtmlOutputLogger">
129+
<parameter name="outputDirectory" value="web/sites/simpletest/browser_output"/>
130+
<parameter name="verbose" value="true"/>
131+
</bootstrap>
132+
</extensions>
127133
</phpunit>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* PHPUnit bootstrap file.
6+
*
7+
* Prepares the environment for running PHPUnit tests.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
// Set the browser test output base URL from the local development URL.
13+
$localdev_url = getenv('VORTEX_LOCALDEV_URL');
14+
if ($localdev_url) {
15+
$base_url = str_starts_with($localdev_url, 'http') ? $localdev_url : 'http://' . $localdev_url;
16+
putenv('BROWSERTEST_OUTPUT_BASE_URL=' . $base_url);
17+
$_SERVER['BROWSERTEST_OUTPUT_BASE_URL'] = $base_url;
18+
$_ENV['BROWSERTEST_OUTPUT_BASE_URL'] = $base_url;
19+
}
20+
21+
// @see https://www.drupal.org/project/drupal/issues/2992069
22+
$browser_output_dir = dirname(__DIR__, 2) . '/web/sites/simpletest/browser_output';
23+
if (!is_dir($browser_output_dir)) {
24+
mkdir($browser_output_dir, 0775, TRUE);
25+
}
26+
27+
// Load the Drupal core test bootstrap.
28+
require dirname(__DIR__, 2) . '/web/core/tests/bootstrap.php';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\Tests\sw_base\FunctionalJavascript;
6+
7+
use PHPUnit\Framework\Attributes\Group;
8+
9+
/**
10+
* Class ExampleTest.
11+
*
12+
* Example functional JavaScript test case class.
13+
*
14+
* @package Drupal\sw_base\Tests
15+
*/
16+
#[Group('SwBase')]
17+
class ExampleTest extends SwBaseFunctionalJavascriptTestBase {
18+
19+
/**
20+
* Test that a page can be loaded and JavaScript is functional.
21+
*/
22+
public function testPageLoad(): void {
23+
$this->drupalGet('<front>');
24+
25+
// Verify that the page loaded by checking for a page element.
26+
$result = $this->assertSession()->waitForElement('css', 'html');
27+
$this->assertNotNull($result, 'Page HTML element is present.');
28+
29+
$this->takeScreenshot('module_page_load');
30+
}
31+
32+
/**
33+
* Test that JavaScript can be executed in the browser.
34+
*/
35+
public function testJavascriptExecution(): void {
36+
$this->drupalGet('<front>');
37+
38+
// Execute JavaScript and verify the result.
39+
$result = $this->getSession()->evaluateScript('1 + 1');
40+
$this->assertEquals(2, $result);
41+
42+
$this->takeScreenshot('module_js_execution');
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\Tests\sw_base\FunctionalJavascript;
6+
7+
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8+
use Drupal\Tests\sw_base\Traits\ArrayTrait;
9+
use Drupal\Tests\sw_base\Traits\AssertTrait;
10+
use Drupal\Tests\sw_base\Traits\BrowserHtmlDebugTrait;
11+
use Drupal\Tests\sw_base\Traits\MockTrait;
12+
use Drupal\Tests\sw_base\Traits\ReflectionTrait;
13+
14+
/**
15+
* Class SwBaseFunctionalJavascriptTestBase.
16+
*
17+
* Base class for functional JavaScript tests.
18+
*
19+
* @package Drupal\sw_base\Tests
20+
*/
21+
abstract class SwBaseFunctionalJavascriptTestBase extends WebDriverTestBase {
22+
23+
use ArrayTrait;
24+
use AssertTrait;
25+
use BrowserHtmlDebugTrait;
26+
use MockTrait;
27+
use ReflectionTrait;
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected $defaultTheme = 'stark';
33+
34+
}

0 commit comments

Comments
 (0)