You are here

public function PiwikBasicTest::testPiwikConfiguration in Piwik Web Analytics 8

Tests if configuration is possible.

File

src/Tests/PiwikBasicTest.php, line 50

Class

PiwikBasicTest
Test basic functionality of Piwik module.

Namespace

Drupal\piwik\Tests

Code

public function testPiwikConfiguration() {

  // Check for setting page's presence.
  $this
    ->drupalGet('admin/config/system/piwik');
  $this
    ->assertRaw(t('Piwik site ID'), '[testPiwikConfiguration]: Settings page displayed.');

  // Check for account code validation.
  $edit['piwik_site_id'] = $this
    ->randomMachineName(2);
  $edit['piwik_url_http'] = 'http://www.example.com/piwik/';
  $this
    ->drupalPostForm('admin/config/system/piwik', $edit, 'Save configuration');
  $this
    ->assertRaw(t('A valid Piwik site ID is an integer only.'), '[testPiwikConfiguration]: Invalid Piwik site ID number validated.');

  // Verify that invalid URLs throw a form error.
  $edit = [];
  $edit['piwik_site_id'] = 1;
  $edit['piwik_url_http'] = 'http://www.example.com/piwik/';
  $edit['piwik_url_https'] = 'https://www.example.com/piwik/';
  $this
    ->drupalPostForm('admin/config/system/piwik', $edit, t('Save configuration'));
  $this
    ->assertRaw('The validation of "http://www.example.com/piwik/piwik.php" failed with an exception', '[testPiwikConfiguration]: HTTP URL exception shown.');
  $this
    ->assertRaw('The validation of "https://www.example.com/piwik/piwik.php" failed with an exception', '[testPiwikConfiguration]: HTTPS URL exception shown.');

  // User should have access to code snippets.
  $this
    ->assertFieldByName('piwik_codesnippet_before');
  $this
    ->assertFieldByName('piwik_codesnippet_after');
  $this
    ->assertNoFieldByXPath("//textarea[@name='piwik_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is enabled.');
  $this
    ->assertNoFieldByXPath("//textarea[@name='piwik_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is enabled.');

  // Login as user without JS permissions.
  $this
    ->drupalLogin($this->noSnippetUser);
  $this
    ->drupalGet('admin/config/system/piwik');

  // User should *not* have access to snippets, but create fields.
  $this
    ->assertFieldByName('piwik_codesnippet_before');
  $this
    ->assertFieldByName('piwik_codesnippet_after');
  $this
    ->assertFieldByXPath("//textarea[@name='piwik_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is disabled.');
  $this
    ->assertFieldByXPath("//textarea[@name='piwik_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is disabled.');
}