1<?php
2
3
4class action_plugin_googletagmanagerTest extends DokuWikiTest
5{
6
7    const gtmPluginName = 'googletagmanager';
8
9    public function setUp()
10    {
11        $this->pluginsEnabled[] = self::gtmPluginName;
12        parent::setUp();
13    }
14
15    public function test_google_tag_manager()
16    {
17
18        global $conf;
19        $gtmValue = "GTM-12345";
20        $conf['plugin'][self::gtmPluginName][action_plugin_googletagmanager::GTMID] = $gtmValue;
21
22        $pageId = 'start';
23        saveWikiText($pageId, "Content", 'Script Test base');
24        idx_addPage($pageId);
25
26        $request = new TestRequest();
27        $response = $request->get(array('id' => $pageId, '/doku.php'));
28
29        /**
30         * Tags to searched
31         */
32        $tagsSearched = ["script", "noscript"];
33
34        foreach ($tagsSearched as $tagSearched) {
35
36            $domElements = $response->queryHTML($tagSearched)->get();
37
38            $patternFound = 0;
39            foreach ($domElements as $domElement) {
40                /**
41                 * @var DOMElement $domElement
42                 */
43                if ($tagSearched=="script") {
44                    $value = $domElement->textContent;
45                } else {
46                    // iframe src
47                    $value = $domElement->firstChild->getAttribute("src");
48                }
49                $patternFound = preg_match("/$gtmValue/i", $value);
50                if ($patternFound === 1) {
51                    break;
52                }
53            }
54            $this->assertEquals(1, $patternFound, "The GTM scripts have been found for the tag $tagSearched");
55        }
56
57
58    }
59
60}
61