Loading tests/src/Functional/FacebookPixelFunctionalTests.php +45 −1 Original line number Diff line number Diff line Loading @@ -43,7 +43,7 @@ class FacebookPixelFunctionalTests extends BrowserTestBase { * * @var string */ protected $fbPixelKey = 'xxxxxxxxxx'; protected $fbPixelKey = '0000000'; /** * {@inheritdoc} Loading @@ -68,6 +68,35 @@ class FacebookPixelFunctionalTests extends BrowserTestBase { $this->drupalLogin($this->user); } /** * Tests if the module installation, won't break the site. */ public function testInstallation() { $session = $this->assertSession(); $this->drupalGet('<front>'); $session->statusCodeEquals(200); } /** * Tests if uninstalling the module, won't break the site. */ public function testUninstallation() { $this->drupalLogout(); $this->drupalLogin($this->adminuser); // Go to uninstallation page an uninstall facebook_pixel: $session = $this->assertSession(); $page = $this->getSession()->getPage(); $this->drupalGet('/admin/modules/uninstall'); $session->statusCodeEquals(200); $page->checkField('edit-uninstall-facebook-pixel'); $page->pressButton('edit-submit'); $session->statusCodeEquals(200); // Confirm deinstall: $page->pressButton('edit-submit'); $session->statusCodeEquals(200); $session->pageTextContains('The selected modules have been uninstalled.'); } /** * Tests if the config form is unaccessible as an anonymous user. */ Loading Loading @@ -149,6 +178,21 @@ class FacebookPixelFunctionalTests extends BrowserTestBase { $session->elementExists('css', 'noscript>img[src*= ' . $this->fbPixelKey . ']'); } /** * Test if disabling the no script fallback, will remove the noscript element. */ public function testDisableNoScriptFallback() { $session = $this->assertSession(); // Set test settings: $this->config('facebook_pixel.settings')->set('visibility.request_path_pages', '')->save(); $this->config('facebook_pixel.settings')->set('facebook_id', $this->fbPixelKey)->save(); $this->config('facebook_pixel.settings')->set('privacy.disable_noscript_img', 'true')->save(); $this->drupalGet('<front>'); $session->statusCodeEquals(200); $session->elementNotExists('css', 'noscript>img[src*= ' . $this->fbPixelKey . ']'); } /** * Tests if the script is not loaded on a specific path with a slash. */ Loading tests/src/FunctionalJavascript/FacebookPixelFunctionalJavascriptTest.php 0 → 100644 +115 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\facebook_pixel\FunctionalJavascript; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; /** * Tests the facebook_pixel javascript functionalities. * * @group facebook_pixel */ class FacebookPixelFunctionalJavascriptTest extends WebDriverTestBase { /** * Modules to enable. * * @var array */ protected static $modules = [ 'test_page_test', 'node', 'facebook_pixel', ]; /** * A user with admin permissions. * * @var \Drupal\Core\Session\AccountInterface */ protected $adminUser; /** * A user with authenticated permissions. * * @var \Drupal\Core\Session\AccountInterface */ protected $user; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->config('system.site')->set('page.front', '/test-page')->save(); $this->user = $this->drupalCreateUser([]); $this->adminUser = $this->drupalCreateUser([]); $this->adminUser->addRole($this->createAdminRole('admin', 'admin')); $this->adminUser->save(); $this->drupalLogin($this->adminUser); } /** * Tests if the FB Pixel script is loaded. */ public function testFbPixelJsLoaded() { $session = $this->assertSession(); // Activate fb Pixel globally: $this->config('facebook_pixel.settings')->set('visibility.request_path_pages', '')->save(); $this->config('facebook_pixel.settings')->set('facebook_id', '0000000')->save(); $this->drupalGet('<front>'); $session->elementExists('css', 'script[src*="facebook_pixel.js"]'); } /** * Tests the testAdvancedOptOut functionality. */ public function testAdvancedOptOut() { $session = $this->assertSession(); /** * @var \Behat\Mink\Driver\Selenium2Driver $driver */ $driver = $this->getSession()->getDriver(); // Set test settings: $this->config('facebook_pixel.settings')->set('visibility.request_path_pages', '')->save(); $this->config('facebook_pixel.settings')->set('facebook_id', '0000000')->save(); $this->config('facebook_pixel.settings')->set('privacy.fb_disable_advanced', 'true')->save(); // Go to front and check if the script is loaded: $this->drupalGet('<front>'); $session->elementExists('css', 'script[src*="facebook_pixel.js"]'); // Fire fbOptout script: $script = "fbOptout();"; $driver->executeScript($script); drupal_flush_all_caches(); // Check if non tracking cookie is set and has the correct value: $cookie = $driver->getCookie('fb-disable'); $this->assertSame('true', $cookie); } public function testDisableNoScriptFallback() { $session = $this->assertSession(); /** * @var \Behat\Mink\Driver\Selenium2Driver $driver */ $driver = $this->getSession()->getDriver(); // Set test settings: $this->config('facebook_pixel.settings')->set('visibility.request_path_pages', '')->save(); $this->config('facebook_pixel.settings')->set('facebook_id', '0000000')->save(); $this->config('facebook_pixel.settings')->set('privacy.disable_noscript_img', 'true')->save(); } } Loading
tests/src/Functional/FacebookPixelFunctionalTests.php +45 −1 Original line number Diff line number Diff line Loading @@ -43,7 +43,7 @@ class FacebookPixelFunctionalTests extends BrowserTestBase { * * @var string */ protected $fbPixelKey = 'xxxxxxxxxx'; protected $fbPixelKey = '0000000'; /** * {@inheritdoc} Loading @@ -68,6 +68,35 @@ class FacebookPixelFunctionalTests extends BrowserTestBase { $this->drupalLogin($this->user); } /** * Tests if the module installation, won't break the site. */ public function testInstallation() { $session = $this->assertSession(); $this->drupalGet('<front>'); $session->statusCodeEquals(200); } /** * Tests if uninstalling the module, won't break the site. */ public function testUninstallation() { $this->drupalLogout(); $this->drupalLogin($this->adminuser); // Go to uninstallation page an uninstall facebook_pixel: $session = $this->assertSession(); $page = $this->getSession()->getPage(); $this->drupalGet('/admin/modules/uninstall'); $session->statusCodeEquals(200); $page->checkField('edit-uninstall-facebook-pixel'); $page->pressButton('edit-submit'); $session->statusCodeEquals(200); // Confirm deinstall: $page->pressButton('edit-submit'); $session->statusCodeEquals(200); $session->pageTextContains('The selected modules have been uninstalled.'); } /** * Tests if the config form is unaccessible as an anonymous user. */ Loading Loading @@ -149,6 +178,21 @@ class FacebookPixelFunctionalTests extends BrowserTestBase { $session->elementExists('css', 'noscript>img[src*= ' . $this->fbPixelKey . ']'); } /** * Test if disabling the no script fallback, will remove the noscript element. */ public function testDisableNoScriptFallback() { $session = $this->assertSession(); // Set test settings: $this->config('facebook_pixel.settings')->set('visibility.request_path_pages', '')->save(); $this->config('facebook_pixel.settings')->set('facebook_id', $this->fbPixelKey)->save(); $this->config('facebook_pixel.settings')->set('privacy.disable_noscript_img', 'true')->save(); $this->drupalGet('<front>'); $session->statusCodeEquals(200); $session->elementNotExists('css', 'noscript>img[src*= ' . $this->fbPixelKey . ']'); } /** * Tests if the script is not loaded on a specific path with a slash. */ Loading
tests/src/FunctionalJavascript/FacebookPixelFunctionalJavascriptTest.php 0 → 100644 +115 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\facebook_pixel\FunctionalJavascript; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; /** * Tests the facebook_pixel javascript functionalities. * * @group facebook_pixel */ class FacebookPixelFunctionalJavascriptTest extends WebDriverTestBase { /** * Modules to enable. * * @var array */ protected static $modules = [ 'test_page_test', 'node', 'facebook_pixel', ]; /** * A user with admin permissions. * * @var \Drupal\Core\Session\AccountInterface */ protected $adminUser; /** * A user with authenticated permissions. * * @var \Drupal\Core\Session\AccountInterface */ protected $user; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->config('system.site')->set('page.front', '/test-page')->save(); $this->user = $this->drupalCreateUser([]); $this->adminUser = $this->drupalCreateUser([]); $this->adminUser->addRole($this->createAdminRole('admin', 'admin')); $this->adminUser->save(); $this->drupalLogin($this->adminUser); } /** * Tests if the FB Pixel script is loaded. */ public function testFbPixelJsLoaded() { $session = $this->assertSession(); // Activate fb Pixel globally: $this->config('facebook_pixel.settings')->set('visibility.request_path_pages', '')->save(); $this->config('facebook_pixel.settings')->set('facebook_id', '0000000')->save(); $this->drupalGet('<front>'); $session->elementExists('css', 'script[src*="facebook_pixel.js"]'); } /** * Tests the testAdvancedOptOut functionality. */ public function testAdvancedOptOut() { $session = $this->assertSession(); /** * @var \Behat\Mink\Driver\Selenium2Driver $driver */ $driver = $this->getSession()->getDriver(); // Set test settings: $this->config('facebook_pixel.settings')->set('visibility.request_path_pages', '')->save(); $this->config('facebook_pixel.settings')->set('facebook_id', '0000000')->save(); $this->config('facebook_pixel.settings')->set('privacy.fb_disable_advanced', 'true')->save(); // Go to front and check if the script is loaded: $this->drupalGet('<front>'); $session->elementExists('css', 'script[src*="facebook_pixel.js"]'); // Fire fbOptout script: $script = "fbOptout();"; $driver->executeScript($script); drupal_flush_all_caches(); // Check if non tracking cookie is set and has the correct value: $cookie = $driver->getCookie('fb-disable'); $this->assertSame('true', $cookie); } public function testDisableNoScriptFallback() { $session = $this->assertSession(); /** * @var \Behat\Mink\Driver\Selenium2Driver $driver */ $driver = $this->getSession()->getDriver(); // Set test settings: $this->config('facebook_pixel.settings')->set('visibility.request_path_pages', '')->save(); $this->config('facebook_pixel.settings')->set('facebook_id', '0000000')->save(); $this->config('facebook_pixel.settings')->set('privacy.disable_noscript_img', 'true')->save(); } }