Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TrackerLoggingTest.php « System « tests « Monolog « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 44a7755e9f574b047505199666660c87899c2df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\Monolog\tests\System;

use Piwik\Config;
use Piwik\Date;
use Piwik\Plugins\Monolog\Handler\EchoHandler;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\SystemTestCase;
use Piwik\Tests\Framework\TestingEnvironmentVariables;
use PiwikTracker;
use Psr\Container\ContainerInterface;

/**
 * @group Monolog
 * @group TrackerLoggingTest
 * @group Plugins
 */
class TrackerLoggingTest extends SystemTestCase
{
    private $idSite = 1;

    public function setUp()
    {
        parent::setUp();

        if (!Fixture::siteCreated($this->idSite)) {
            Fixture::createWebsite('2014-01-01 00:00:00');
        }
    }

    public function test_shouldReturnDebugOutput_IfDebugIsEnabled()
    {
        $this->setTrackerConfig(array('debug' => '1'));

        $tracker = $this->buildTracker();
        $this->assertTrackerResponseContainsLogOutput($tracker);
    }

    public function test_shouldReturnDebugOutput_IfDebugOnDemandIsEnabled()
    {
        $this->setTrackerConfig(array('debug_on_demand' => '1', 'debug' => 0));

        $tracker = $this->buildTracker();
        $tracker->setDebugStringAppend('debug=1');
        $this->assertTrackerResponseContainsLogOutput($tracker);
    }

    public function test_shouldNotReturnDebugOutput_IfDebugOnDemandIsDisabled()
    {
        $this->setTrackerConfig(array('debug_on_demand' => '0', 'debug' => 0));

        $tracker = $this->buildTracker();
        $tracker->setDebugStringAppend('debug=1');

        Fixture::checkResponse($tracker->doTrackPageView('incredible title!'));
    }

    private function buildTracker()
    {
        $t = Fixture::getTracker($this->idSite, Date::factory('2014-01-05 00:01:01')->getDatetime());
        $t->setDebugStringAppend('debug=1');
        $t->setTokenAuth(Fixture::getTokenAuth());
        $t->setUrl('http://example.org/index1.htm');

        return $t;
    }

    private function assertTrackerResponseContainsLogOutput(PiwikTracker $t)
    {
        $response = $t->doTrackPageView('incredible title!');

        $this->assertStringStartsWith("DEBUG: Debug enabled - Input parameters: array (
  'idsite' => '1',
  'rec' => '1',
  'apiv' => '1',", $response);
    }

    private function setTrackerConfig($trackerConfig)
    {
        $testingEnvironment = new TestingEnvironmentVariables();
        $testingEnvironment->overrideConfig('Tracker', $trackerConfig);
        $testingEnvironment->overrideConfig('log', 'log_writers', array('screen'));
        $testingEnvironment->save();
    }

    public static function provideContainerConfigBeforeClass()
    {
        return array(
            'Psr\Log\LoggerInterface' => \DI\get('Monolog\Logger'),
            Config::class => \DI\decorate(function (Config $config) {
                $config->tests['enable_logging'] = 1;
                $config->log['log_writers'] = ['screen'];
                return $config;
            }),
            'Tests.log.allowAllHandlers' => true,
        );
    }

}