MatomoSearchTest.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Drupal\matomo\Tests;
  3. use Drupal\simpletest\WebTestBase;
  4. /**
  5. * Test search functionality of Matomo module.
  6. *
  7. * @group Matomo
  8. */
  9. class MatomoSearchTest extends WebTestBase {
  10. /**
  11. * Modules to enable.
  12. *
  13. * @var array
  14. */
  15. public static $modules = ['matomo', 'search', 'node'];
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected function setUp() {
  20. parent::setUp();
  21. $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
  22. $permissions = [
  23. 'access administration pages',
  24. 'administer matomo',
  25. 'search content',
  26. 'create page content',
  27. 'edit own page content',
  28. ];
  29. // User to set up matomo.
  30. $this->admin_user = $this->drupalCreateUser($permissions);
  31. $this->drupalLogin($this->admin_user);
  32. }
  33. /**
  34. * Tests if search tracking is properly added to the page.
  35. */
  36. public function testMatomoSearchTracking() {
  37. $site_id = '1';
  38. $this->config('matomo.settings')->set('site_id', $site_id)->save();
  39. $this->config('matomo.settings')->set('url_http', 'http://www.example.com/matomo/')->save();
  40. $this->config('matomo.settings')->set('url_https', 'https://www.example.com/matomo/')->save();
  41. // Check tracking code visibility.
  42. $this->drupalGet('');
  43. $this->assertRaw($site_id, '[testMatomoSearch]: Tracking code is displayed for authenticated users.');
  44. $this->drupalGet('search/node');
  45. $this->assertNoRaw('_paq.push(["trackSiteSearch", ', '[testMatomoSearch]: Search tracker not added to page.');
  46. // Enable site search support.
  47. $this->config('matomo.settings')->set('track.site_search', 1)->save();
  48. // Search for random string.
  49. $search = [];
  50. $search['keys'] = $this->randomMachineName(8);
  51. // Create a node to search for.
  52. $edit = [];
  53. $edit['title[0][value]'] = 'This is a test title';
  54. $edit['body[0][value]'] = 'This test content contains ' . $search['keys'] . ' string.';
  55. // Fire a search, it's expected to get 0 results.
  56. $this->drupalPostForm('search/node', $search, t('Search'));
  57. $this->assertRaw('_paq.push(["trackSiteSearch", ', '[testMatomoSearch]: Search results tracker is displayed.');
  58. $this->assertRaw('window.matomo_search_results = 0;', '[testMatomoSearch]: Search yielded no results.');
  59. // Save the node.
  60. $this->drupalPostForm('node/add/page', $edit, t('Save'));
  61. $this->assertText(t('@type @title has been created.', ['@type' => 'Basic page', '@title' => $edit['title[0][value]']]), 'Basic page created.');
  62. // Index the node or it cannot found.
  63. $this->cronRun();
  64. $this->drupalPostForm('search/node', $search, t('Search'));
  65. $this->assertRaw('_paq.push(["trackSiteSearch", ', '[testMatomoSearch]: Search results tracker is displayed.');
  66. $this->assertRaw('window.matomo_search_results = 1;', '[testMatomoSearch]: One search result found.');
  67. $this->drupalPostForm('node/add/page', $edit, t('Save'));
  68. $this->assertText(t('@type @title has been created.', ['@type' => 'Basic page', '@title' => $edit['title[0][value]']]), 'Basic page created.');
  69. // Index the node or it cannot found.
  70. $this->cronRun();
  71. $this->drupalPostForm('search/node', $search, t('Search'));
  72. $this->assertRaw('_paq.push(["trackSiteSearch", ', '[testMatomoSearch]: Search results tracker is displayed.');
  73. $this->assertRaw('window.matomo_search_results = 2;', '[testMatomoSearch]: Two search results found.');
  74. }
  75. }