Commit 9eb59b96 authored by Josha Hubbers's avatar Josha Hubbers Committed by Josha Hubbers
Browse files

Issue #3267590 by JoshaHubbers: Add tests

parent a8af26da
Loading
Loading
Loading
Loading
+183 −0
Changes for tests/src/Functional/PiwikProSnippetTest.php: 183 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\piwik_pro\Functional;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;

/**
 * Basic testing of the Piwik Pro snippet.
 *
 * @group piwik_pro
 */
class PiwikProSnippetTest extends BrowserTestBase {

  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Admin user.
   *
   * @var \Drupal\user\Entity\User|bool
   */
  protected $adminUser;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'piwik_pro',
    'user',
    'help',
    'block',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() :void {
    parent::setUp();

    // Set basic config.
    $this->config = $this->config('piwik_pro.settings');
    $this->config
      ->set('site_id', '00000000-0000-1000-a000-000000000000')
      ->set('piwik_domain', 'https://yourname.containers.piwik.pro/')
      ->set('sync_snippet', 0)
      ->set('data_layer', 'dataLayer')
      ->set('visibility.request_path_mode', 0)
      ->set('visibility.request_path_pages', "/admin\n/admin/*\n/batch\n/node/add*\n/node/*/*\n/user/*/*")
      ->save();

    // Setup and login admin user.
    $permissions = [
      'access administration pages',
      'administer piwik pro',
      'administer modules',
      'administer site configuration',
    ];
    $this->adminUser = $this->drupalCreateUser($permissions);
    $this->drupalLogin($this->adminUser);

    // Place the block to show help.
    $this->drupalPlaceBlock('help_block', ['region' => 'help']);
  }

  /**
   * Tests if configuration is possible.
   */
  public function testPiwikProConfiguration() {
    // Check if Configure link is available on 'Extend' page.
    $this->drupalGet('admin/modules');
    $this->assertSession()->responseContains('admin/config/services/piwik-pro');

    // Check for admin page availability.
    $this->drupalGet('admin/config/services/piwik-pro');
    $this->assertSession()->responseContains($this->t('Container address (URL)'));
  }

  /**
   * Tests if help sections are shown.
   */
  public function testPiwikProHelp() {
    // Test help on admin page.
    $this->drupalGet('admin/config/services/piwik-pro');
    $this->assertSession()->pageTextContains('Piwik Pro is a GDPR-proof analytics tool.');

    // Test module help-page.
    $this->drupalGet('admin/help/piwik_pro');
    $this->assertSession()->pageTextContains('Piwik Pro is a GDPR-proof tracking tool that allows you to track user visits.');
  }

  /**
   * Test if the snippet shows with setting "Show on all pages except listed",
   * and without sync-snippet.
   */
  public function testSnippetVisibility() {
    // Default should show on the homepage.
    $this->drupalGet('');
    $async_url = sprintf('%s%s/noscript.html',
      $this->config->get('piwik_domain'),
      $this->config->get('site_id'));
    $this->assertSession()->responseContains($async_url);
    $this->assertSession()->responseContains($this->config->get('data_layer'));

    // Default the sync-script is not shown.
    $sync_url = sprintf('%s\'+id+\'.sync.js',
      $this->config->get('piwik_domain'));
    $this->assertSession()->responseNotContains($sync_url);

    // Default should not show on admin pages.
    $this->drupalGet('admin/modules');
    $this->assertSession()->responseNotContains($this->config->get('piwik_domain'));
  }

  /**
   * Test if the snippet shows with setting "Show only on these pages".
   */
  public function testSnippetVisibilityPagesInverted() {
    $this->config->set('visibility.request_path_mode', 1)->save();
    $this->refreshVariables();
    // Now not shown on the front page.
    $this->drupalGet('');
    $this->assertSession()->responseNotContains($this->config->get('piwik_domain'));
    // But does show on admin/modules.
    $this->drupalGet('admin/modules');
    $this->assertSession()->responseContains($this->config->get('piwik_domain'));
  }

  /**
   * Test if the sync-snippet shows when this is set.
   */
  public function testSnippetVisibilitySyncSnippet() {
    $this->config->set('sync_snippet', 1)->save();
    $this->refreshVariables();

    $this->drupalGet('');
    $sync_url = sprintf('%s\'+id+\'.sync.js',
      $this->config->get('piwik_domain'));
    $this->assertSession()->responseContains($sync_url);
  }

  /**
   * Test if the datalayer is changed when set in config.
   */
  public function testSnippetVisibilityChangedDataLayer() {
    $this->config->set('data_layer', 'changedDataLayer')->save();
    $this->refreshVariables();

    $this->drupalGet('');
    $new_datalayer = sprintf('(window, document, \'%s\', \'%s\')',
    'changedDataLayer',
      $this->config->get('site_id'));
    $this->assertSession()->responseContains($new_datalayer);
  }

  /**
   * Test the settings-form.
   */
  public function testSettingsForm() {
    $this->drupalGet('/admin/config/services/piwik-pro');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Piwik Pro');
    $this->assertSession()->elementExists('css', '#edit-site-id');

    $edit = [
      'site_id' => '0',
      'data_layer' => '0'
    ];
    $this->submitForm($edit, $this->t('Save configuration'));
    $this->assertSession()->responseContains($this->t('Invalid <code>Site ID</code> value.'));
    $this->assertSession()->responseContains($this->t('Invalid <code>Data layer</code> value.'));
  }

}