-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UHF-10908: Added test for the cookie banner functionality.
- Loading branch information
Showing
1 changed file
with
112 additions
and
0 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
modules/hdbt_cookie_banner/tests/src/FunctionalJavascript/CookieBannerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\hdbt_cookie_banner\FunctionalJavascript; | ||
|
||
use Drupal\FunctionalJavascriptTests\WebDriverTestBase; | ||
|
||
/** | ||
* Tests the functionality of the JavaScript cookie banner. | ||
* | ||
* @group hdbt_cookie_banner | ||
*/ | ||
class CookieBannerTest extends WebDriverTestBase { | ||
|
||
/** | ||
* Modules to enable. | ||
* | ||
* @var array | ||
*/ | ||
protected static $modules = [ | ||
'hdbt_cookie_banner', | ||
'hdbt_cookie_banner_test', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $defaultTheme = 'stark'; | ||
|
||
/** | ||
* Setup the test environment. | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
// Get the path to the JSON file. | ||
$module_path = \Drupal::service('extension.list.module') | ||
->getPath('hdbt_cookie_banner'); | ||
$json_file_path = $module_path . '/assets/json/siteSettingsTemplate.json'; | ||
|
||
// Assert the file exists. | ||
$this->assertTrue(file_exists($json_file_path)); | ||
|
||
// Load and decode the JSON. | ||
$json_content = file_get_contents($json_file_path); | ||
$this->assertNotEmpty($json_content, 'Decoded JSON data is not empty.'); | ||
|
||
// Get the public base URL (in a FunctionalJavascript test). | ||
// Construct a URL for the hds-cookie-consent.min.js file. | ||
$cookie_js_url = "/$module_path/assets/js/hds-cookie-consent.min.js"; | ||
|
||
// Change configuration value before the test runs. | ||
$config = $this->config('hdbt_cookie_banner.settings'); | ||
$config | ||
->set('use_custom_settings', TRUE) | ||
->set('site_settings', $json_content) | ||
->set('use_internal_hds_cookie_js', FALSE) | ||
->set('hds_cookie_js_override', $cookie_js_url) | ||
->save(); | ||
|
||
\Drupal::service('cache.default')->deleteAll(); | ||
} | ||
|
||
/** | ||
* Tests the cookie banner visibility and interaction. | ||
*/ | ||
public function testCookieBanner() { | ||
|
||
$this->drupalGet('/test-page'); | ||
$this->assertSession()->pageTextContains('Test Content'); | ||
$this->assertSession()->elementExists('css', '.test-footer'); | ||
|
||
// phpcs:disable | ||
// Take a screenshot. | ||
// $this->createScreenshot(\Drupal::root() .'/sites/default/files/simpletest/test.png'); | ||
|
||
// Get browser logs (console output). | ||
// $logs = $this->getSession()->getDriver()->getWebDriverSession()->log('browser'); | ||
// foreach ($logs as $log) { | ||
// var_dump( "JavaScript Log: " . $log['message']); | ||
// } | ||
|
||
// Debug the page html. | ||
// $page = $this->getSession()->getPage(); | ||
// $this->htmlOutput($page->getHtml()); | ||
// phpcs:enable | ||
|
||
// Get the Shadow DOM host and button selectors. | ||
$shadowHostSelector = '.hds-cc__target'; | ||
$buttonSelector = '.hds-cc__all-cookies-button'; | ||
|
||
// JavaScript to locate the button inside the Shadow DOM and click it. | ||
$js = <<<JS | ||
const shadowHost = document.querySelector('$shadowHostSelector'); | ||
if (!shadowHost) { | ||
throw new Error('Shadow host not found.'); | ||
} | ||
const shadowRoot = shadowHost.shadowRoot; | ||
if (!shadowRoot) { | ||
throw new Error('Shadow root is not attached.'); | ||
} | ||
const button = shadowRoot.querySelector('$buttonSelector'); | ||
if (!button) { | ||
throw new Error('Button not found inside the shadow DOM.'); | ||
} | ||
button.click(); | ||
JS; | ||
|
||
// Execute the JavaScript in the browser context. | ||
$this->getSession()->executeScript($js); | ||
} | ||
|
||
} |