Loading src/AssetInjectorListBuilder.php +3 −0 Original line number Diff line number Diff line Loading @@ -47,6 +47,7 @@ class AssetInjectorListBuilder extends ConfigEntityListBuilder { public function buildHeader() { $header['label'] = $this->t('Injector'); $header['conditions'] = $this->t('Conditions'); $header['status'] = $this->t('Status'); return $header + parent::buildHeader(); } Loading Loading @@ -82,6 +83,8 @@ class AssetInjectorListBuilder extends ConfigEntityListBuilder { ]; $data['conditions'] = $this->renderer->render($data['conditions']); $data['status'] = $entity->status() ? $this->t('Enabled') : $this->t('Disabled'); $row = [ 'class' => $entity->status() ? 'enabled' : 'disabled', 'data' => $data + parent::buildRow($entity), Loading src/Form/AssetInjectorFormBase.php +6 −0 Original line number Diff line number Diff line Loading @@ -151,6 +151,12 @@ class AssetInjectorFormBase extends EntityForm { '#disabled' => !$entity->isNew(), ]; $form['status'] = [ '#type' => 'checkbox', '#title' => $this->t('Enabled'), '#default_value' => $this->entity->status(), ]; $form['code'] = [ '#type' => 'textarea', '#title' => $this->t('Code'), Loading tests/src/Functional/AssetInjectorCssTest.php +38 −5 Original line number Diff line number Diff line Loading @@ -2,11 +2,11 @@ namespace Drupal\Tests\asset_injector\Functional; use Drupal\Tests\BrowserTestBase; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Tests\BrowserTestBase; /** * Class AssetInjectorCssTest. * Tests CSS Asset Injector. * * @package Drupal\Tests\asset_injector\Functional * Loading Loading @@ -74,10 +74,10 @@ class AssetInjectorCssTest extends BrowserTestBase { $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextContains($this->t('Code')); $this->submitForm([ 'label' => $this->t('Blocks'), 'id' => $this->t('blocks'), 'label' => 'Blocks', 'id' => 'blocks', 'code' => '.block {border:1px solid black;}', ], $this->t('Save')); ], 'Save'); $this->getSession()->getPage()->hasContent('asset_injector/css/blocks'); /** @var \Drupal\asset_injector\Entity\AssetInjectorCss $asset */ Loading Loading @@ -112,4 +112,37 @@ class AssetInjectorCssTest extends BrowserTestBase { ->addressEquals('admin/config/development/asset-injector/css/test_save_continue'); } /** * Tests if the Form functions correctly. * * @throws \Exception */ public function testForm() { $session = $this->assertSession(); $page = $this->getSession()->getPage(); $this->testCssPermissionGranted(); // Go to the settings page and check the default values. $this->drupalGet('admin/config/development/asset-injector/css/add'); $session->statusCodeEquals(200); $session->fieldValueEquals('label', ''); $session->checkboxChecked('status'); $session->fieldValueEquals('code', ''); $session->fieldValueEquals('media', 'all'); $session->checkboxChecked('preprocess'); // Change all values and save the form. $page->fillField('label', 'test_label'); $page->uncheckField('status'); $page->fillField('code', 'test_code'); $page->fillField('media', 'print'); $page->uncheckField('preprocess'); $page->pressButton('save_continue'); $session->statusCodeEquals(200); // Check if the changed settings still apply. $session->fieldValueEquals('label', 'test_label'); $session->checkboxNotChecked('status'); $session->fieldValueEquals('code', 'test_code'); $session->fieldValueEquals('media', 'print'); $session->checkboxNotChecked('preprocess'); } } tests/src/Functional/AssetInjectorJsTest.php +41 −31 Original line number Diff line number Diff line Loading @@ -2,11 +2,11 @@ namespace Drupal\Tests\asset_injector\Functional; use Drupal\Tests\BrowserTestBase; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Tests\BrowserTestBase; /** * Class AssetInjectorJsTest. * Test JS Asset Injector. * * @package Drupal\Tests\asset_injector\Functional * Loading Loading @@ -70,35 +70,6 @@ class AssetInjectorJsTest extends BrowserTestBase { $this->assertSession()->statusCodeEquals(200); } /** * Test a created css injector is added to the page and the css file exists. * * @throws \Exception */ public function testJsInjector() { $this->testJsPermissionGranted(); $this->drupalGet('admin/config/development/asset-injector/js/add'); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextContains($this->t('Code')); $this->submitForm([ 'label' => $this->t('Blocks'), 'id' => $this->t('blocks'), 'code' => '.block {border:1px solid black;}', ], $this->t('Save')); $this->getSession()->getPage()->hasContent('asset_injector/js/blocks'); /** @var \Drupal\asset_injector\Entity\AssetInjectorJs $asset */ foreach (asset_injector_get_assets(NULL, ['asset_injector_js']) as $asset) { $path = parse_url(\Drupal::service('file_url_generator') ->generateAbsoluteString($asset->internalFileUri()), PHP_URL_PATH); $path = str_replace(base_path(), '/', $path); $this->drupalGet($path); $this->assertSession()->statusCodeEquals(200); } } /** * Tests if the save and continue button works accurately. * Loading @@ -120,4 +91,43 @@ class AssetInjectorJsTest extends BrowserTestBase { ->addressEquals('admin/config/development/asset-injector/js/test_save_continue'); } /** * Tests if the Form functions correctly. * * @throws \Exception */ public function testForm() { $session = $this->assertSession(); $page = $this->getSession()->getPage(); $this->testJsPermissionGranted(); // Go to the settings page and check the default values. $this->drupalGet('admin/config/development/asset-injector/js/add'); $session->statusCodeEquals(200); $session->fieldValueEquals('label', ''); $session->checkboxChecked('status'); $session->fieldValueEquals('code', ''); $session->checkboxNotChecked('jquery'); $session->checkboxChecked('preprocess'); $session->checkboxNotChecked('header'); $session->checkboxNotChecked('use_noscript'); // Change all values and save the form. $page->fillField('label', 'test_label'); $page->uncheckField('status'); $page->fillField('code', 'test_code'); $page->checkField('jquery'); $page->uncheckField('preprocess'); $page->checkField('header'); $page->checkField('use_noscript'); $page->pressButton('save_continue'); $session->statusCodeEquals(200); // Check if the changed settings still apply. $session->fieldValueEquals('label', 'test_label'); $session->checkboxNotChecked('status'); $session->fieldValueEquals('code', 'test_code'); $session->checkboxChecked('jquery'); $session->checkboxNotChecked('preprocess'); $session->checkboxChecked('header'); $session->checkboxChecked('use_noscript'); } } tests/src/FunctionalJavascript/AssetInjectorFunctionalJsTest.php 0 → 100644 +133 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\asset_injector\FunctionalJavascript; use Drupal\asset_injector\Entity\AssetInjectorCss; use Drupal\asset_injector\Entity\AssetInjectorJs; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; use Drupal\Tests\BrowserTestBase; use WebDriver\Exception\UnexpectedAlertOpen; /** * Tests the general Asset Injector functionality. * * @group asset_injector */ final class AssetInjectorFunctionalJsTest extends WebDriverTestBase { /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * {@inheritdoc} */ protected static $modules = ['asset_injector', 'test_page_test']; /** * An admin user. * * @var \Drupal\user\UserInterface */ protected $adminUser; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->config('system.site')->set('page.front', '/test-page')->save(); $this->adminUser = $this->drupalCreateUser([ 'administer css assets injector', 'administer js assets injector', 'administer site configuration', ]); $this->drupalLogin($this->adminUser); } /** * Tests whether injected JS works correctly. */ public function testInjectedJs() { $driver = $this->getSession()->getDriver(); // Create a new JS injector and set an alert for the front page. AssetInjectorJs::create([ 'type' => 'asset_injector_js', 'id' => 'test_js', 'label' => 'test_js', 'status' => TRUE, 'code' => 'alert("Test");', 'conditions' => [ 'request_path' => [ 'id' => 'request_path', 'negate' => FALSE, 'pages' => '<front>', ], ], ])->save(); // Go to a page that should not display the alert. // This call would throw the error from below if the alert was still there. $this->drupalGet('/admin'); // Go to the front page and see if the alert is popping up. try { $this->drupalGet('<front>'); } catch (UnexpectedAlertOpen $e) { $this->assertTrue(TRUE); } $message = $driver->getWebDriverSession()->getAlert_text(); $driver->getWebDriverSession()->accept_alert(); $this->assertEquals('Test', $message); // Disable the injector and check that the alert is gone. AssetInjectorJs::load('test_js')->disable()->save(); // This call would throw the error from above if the alert was still there. $this->drupalGet('<front>'); } /** * Tests wether injected CSS works correctly. */ public function testInjectedCss() { // Create a new CSS injector and set an alert for the front page. AssetInjectorCss::create([ 'type' => 'asset_injector_css', 'id' => 'test_css', 'label' => 'test_css', 'status' => TRUE, 'code' => 'body {background-color: #000000 !important;}', 'conditions' => [ 'request_path' => [ 'id' => 'request_path', 'negate' => FALSE, 'pages' => '<front>', ], ], ])->save(); $script = "(function(){ return window.getComputedStyle(document.body, null).getPropertyValue('background-color') === 'rgb(0, 0, 0)'; })();"; // Go to a page that should not have been modified and check the CSS. $this->drupalGet('/admin'); $this->assertFalse($this->getSession()->evaluateScript($script)); // Go to the front page and see if the CSS is modified. $this->drupalGet('<front>'); $this->assertTrue($this->getSession()->evaluateScript($script)); // Disable the injector and check that the CSS is gone. AssetInjectorCss::load('test_css')->disable()->save(); $this->drupalGet('<front>'); $this->assertFalse($this->getSession()->evaluateScript($script)); } /** * {@inheritdoc} */ protected function tearDown(): void { // Run Grandparent teardown() here, so no unexpected alert // exception gets thrown inside "WebDriverTestBase": BrowserTestBase::tearDown(); } } Loading
src/AssetInjectorListBuilder.php +3 −0 Original line number Diff line number Diff line Loading @@ -47,6 +47,7 @@ class AssetInjectorListBuilder extends ConfigEntityListBuilder { public function buildHeader() { $header['label'] = $this->t('Injector'); $header['conditions'] = $this->t('Conditions'); $header['status'] = $this->t('Status'); return $header + parent::buildHeader(); } Loading Loading @@ -82,6 +83,8 @@ class AssetInjectorListBuilder extends ConfigEntityListBuilder { ]; $data['conditions'] = $this->renderer->render($data['conditions']); $data['status'] = $entity->status() ? $this->t('Enabled') : $this->t('Disabled'); $row = [ 'class' => $entity->status() ? 'enabled' : 'disabled', 'data' => $data + parent::buildRow($entity), Loading
src/Form/AssetInjectorFormBase.php +6 −0 Original line number Diff line number Diff line Loading @@ -151,6 +151,12 @@ class AssetInjectorFormBase extends EntityForm { '#disabled' => !$entity->isNew(), ]; $form['status'] = [ '#type' => 'checkbox', '#title' => $this->t('Enabled'), '#default_value' => $this->entity->status(), ]; $form['code'] = [ '#type' => 'textarea', '#title' => $this->t('Code'), Loading
tests/src/Functional/AssetInjectorCssTest.php +38 −5 Original line number Diff line number Diff line Loading @@ -2,11 +2,11 @@ namespace Drupal\Tests\asset_injector\Functional; use Drupal\Tests\BrowserTestBase; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Tests\BrowserTestBase; /** * Class AssetInjectorCssTest. * Tests CSS Asset Injector. * * @package Drupal\Tests\asset_injector\Functional * Loading Loading @@ -74,10 +74,10 @@ class AssetInjectorCssTest extends BrowserTestBase { $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextContains($this->t('Code')); $this->submitForm([ 'label' => $this->t('Blocks'), 'id' => $this->t('blocks'), 'label' => 'Blocks', 'id' => 'blocks', 'code' => '.block {border:1px solid black;}', ], $this->t('Save')); ], 'Save'); $this->getSession()->getPage()->hasContent('asset_injector/css/blocks'); /** @var \Drupal\asset_injector\Entity\AssetInjectorCss $asset */ Loading Loading @@ -112,4 +112,37 @@ class AssetInjectorCssTest extends BrowserTestBase { ->addressEquals('admin/config/development/asset-injector/css/test_save_continue'); } /** * Tests if the Form functions correctly. * * @throws \Exception */ public function testForm() { $session = $this->assertSession(); $page = $this->getSession()->getPage(); $this->testCssPermissionGranted(); // Go to the settings page and check the default values. $this->drupalGet('admin/config/development/asset-injector/css/add'); $session->statusCodeEquals(200); $session->fieldValueEquals('label', ''); $session->checkboxChecked('status'); $session->fieldValueEquals('code', ''); $session->fieldValueEquals('media', 'all'); $session->checkboxChecked('preprocess'); // Change all values and save the form. $page->fillField('label', 'test_label'); $page->uncheckField('status'); $page->fillField('code', 'test_code'); $page->fillField('media', 'print'); $page->uncheckField('preprocess'); $page->pressButton('save_continue'); $session->statusCodeEquals(200); // Check if the changed settings still apply. $session->fieldValueEquals('label', 'test_label'); $session->checkboxNotChecked('status'); $session->fieldValueEquals('code', 'test_code'); $session->fieldValueEquals('media', 'print'); $session->checkboxNotChecked('preprocess'); } }
tests/src/Functional/AssetInjectorJsTest.php +41 −31 Original line number Diff line number Diff line Loading @@ -2,11 +2,11 @@ namespace Drupal\Tests\asset_injector\Functional; use Drupal\Tests\BrowserTestBase; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Tests\BrowserTestBase; /** * Class AssetInjectorJsTest. * Test JS Asset Injector. * * @package Drupal\Tests\asset_injector\Functional * Loading Loading @@ -70,35 +70,6 @@ class AssetInjectorJsTest extends BrowserTestBase { $this->assertSession()->statusCodeEquals(200); } /** * Test a created css injector is added to the page and the css file exists. * * @throws \Exception */ public function testJsInjector() { $this->testJsPermissionGranted(); $this->drupalGet('admin/config/development/asset-injector/js/add'); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->pageTextContains($this->t('Code')); $this->submitForm([ 'label' => $this->t('Blocks'), 'id' => $this->t('blocks'), 'code' => '.block {border:1px solid black;}', ], $this->t('Save')); $this->getSession()->getPage()->hasContent('asset_injector/js/blocks'); /** @var \Drupal\asset_injector\Entity\AssetInjectorJs $asset */ foreach (asset_injector_get_assets(NULL, ['asset_injector_js']) as $asset) { $path = parse_url(\Drupal::service('file_url_generator') ->generateAbsoluteString($asset->internalFileUri()), PHP_URL_PATH); $path = str_replace(base_path(), '/', $path); $this->drupalGet($path); $this->assertSession()->statusCodeEquals(200); } } /** * Tests if the save and continue button works accurately. * Loading @@ -120,4 +91,43 @@ class AssetInjectorJsTest extends BrowserTestBase { ->addressEquals('admin/config/development/asset-injector/js/test_save_continue'); } /** * Tests if the Form functions correctly. * * @throws \Exception */ public function testForm() { $session = $this->assertSession(); $page = $this->getSession()->getPage(); $this->testJsPermissionGranted(); // Go to the settings page and check the default values. $this->drupalGet('admin/config/development/asset-injector/js/add'); $session->statusCodeEquals(200); $session->fieldValueEquals('label', ''); $session->checkboxChecked('status'); $session->fieldValueEquals('code', ''); $session->checkboxNotChecked('jquery'); $session->checkboxChecked('preprocess'); $session->checkboxNotChecked('header'); $session->checkboxNotChecked('use_noscript'); // Change all values and save the form. $page->fillField('label', 'test_label'); $page->uncheckField('status'); $page->fillField('code', 'test_code'); $page->checkField('jquery'); $page->uncheckField('preprocess'); $page->checkField('header'); $page->checkField('use_noscript'); $page->pressButton('save_continue'); $session->statusCodeEquals(200); // Check if the changed settings still apply. $session->fieldValueEquals('label', 'test_label'); $session->checkboxNotChecked('status'); $session->fieldValueEquals('code', 'test_code'); $session->checkboxChecked('jquery'); $session->checkboxNotChecked('preprocess'); $session->checkboxChecked('header'); $session->checkboxChecked('use_noscript'); } }
tests/src/FunctionalJavascript/AssetInjectorFunctionalJsTest.php 0 → 100644 +133 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\asset_injector\FunctionalJavascript; use Drupal\asset_injector\Entity\AssetInjectorCss; use Drupal\asset_injector\Entity\AssetInjectorJs; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; use Drupal\Tests\BrowserTestBase; use WebDriver\Exception\UnexpectedAlertOpen; /** * Tests the general Asset Injector functionality. * * @group asset_injector */ final class AssetInjectorFunctionalJsTest extends WebDriverTestBase { /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * {@inheritdoc} */ protected static $modules = ['asset_injector', 'test_page_test']; /** * An admin user. * * @var \Drupal\user\UserInterface */ protected $adminUser; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->config('system.site')->set('page.front', '/test-page')->save(); $this->adminUser = $this->drupalCreateUser([ 'administer css assets injector', 'administer js assets injector', 'administer site configuration', ]); $this->drupalLogin($this->adminUser); } /** * Tests whether injected JS works correctly. */ public function testInjectedJs() { $driver = $this->getSession()->getDriver(); // Create a new JS injector and set an alert for the front page. AssetInjectorJs::create([ 'type' => 'asset_injector_js', 'id' => 'test_js', 'label' => 'test_js', 'status' => TRUE, 'code' => 'alert("Test");', 'conditions' => [ 'request_path' => [ 'id' => 'request_path', 'negate' => FALSE, 'pages' => '<front>', ], ], ])->save(); // Go to a page that should not display the alert. // This call would throw the error from below if the alert was still there. $this->drupalGet('/admin'); // Go to the front page and see if the alert is popping up. try { $this->drupalGet('<front>'); } catch (UnexpectedAlertOpen $e) { $this->assertTrue(TRUE); } $message = $driver->getWebDriverSession()->getAlert_text(); $driver->getWebDriverSession()->accept_alert(); $this->assertEquals('Test', $message); // Disable the injector and check that the alert is gone. AssetInjectorJs::load('test_js')->disable()->save(); // This call would throw the error from above if the alert was still there. $this->drupalGet('<front>'); } /** * Tests wether injected CSS works correctly. */ public function testInjectedCss() { // Create a new CSS injector and set an alert for the front page. AssetInjectorCss::create([ 'type' => 'asset_injector_css', 'id' => 'test_css', 'label' => 'test_css', 'status' => TRUE, 'code' => 'body {background-color: #000000 !important;}', 'conditions' => [ 'request_path' => [ 'id' => 'request_path', 'negate' => FALSE, 'pages' => '<front>', ], ], ])->save(); $script = "(function(){ return window.getComputedStyle(document.body, null).getPropertyValue('background-color') === 'rgb(0, 0, 0)'; })();"; // Go to a page that should not have been modified and check the CSS. $this->drupalGet('/admin'); $this->assertFalse($this->getSession()->evaluateScript($script)); // Go to the front page and see if the CSS is modified. $this->drupalGet('<front>'); $this->assertTrue($this->getSession()->evaluateScript($script)); // Disable the injector and check that the CSS is gone. AssetInjectorCss::load('test_css')->disable()->save(); $this->drupalGet('<front>'); $this->assertFalse($this->getSession()->evaluateScript($script)); } /** * {@inheritdoc} */ protected function tearDown(): void { // Run Grandparent teardown() here, so no unexpected alert // exception gets thrown inside "WebDriverTestBase": BrowserTestBase::tearDown(); } }