File: CoreArchiverProcessSignal.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (168 lines) | stat: -rw-r--r-- 4,556 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

declare(strict_types=1);

namespace Piwik\Plugins\CoreAdminHome\tests\Fixtures;

use Closure;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Config;
use Piwik\Container\Container;
use Piwik\Date;
use Piwik\DI;
use Piwik\Plugins\CoreAdminHome\tests\Fixtures\CoreArchiverProcessSignal\StepControl;
use Piwik\Plugins\Monolog\Handler\EchoHandler;
use Piwik\Plugins\SegmentEditor\API as APISegmentEditor;
use Piwik\Request;
use Piwik\Tests\Framework\Fixture;

/**
 * Fixture that adds one site and tracks one pageview for today.
 *
 * Provides container configuration and helpers to run process signal tests.
 */
class CoreArchiverProcessSignal extends Fixture
{
    public const ENV_TRIGGER = 'MATOMO_TEST_CORE_ARCHIVER_PROCESS_SIGNAL';

    public const TEST_SEGMENT_CH = 'browserCode==CH';
    public const TEST_SEGMENT_FF = 'browserCode==FF';

    /**
     * @var int
     */
    public $idSite = 1;

    /**
     * @var StepControl
     */
    public $stepControl;

    /**
     * @var string
     */
    public $today;

    /**
     * @var bool
     */
    private $inTestEnv;

    /**
     * @var bool
     */
    private $inTestRequest;

    public function __construct()
    {
        $this->inTestEnv = (bool) getenv(self::ENV_TRIGGER);
        $this->inTestRequest = Request::fromRequest()->getBoolParameter(self::ENV_TRIGGER, false);
        $this->today = '2024-05-03';

        $this->stepControl = new StepControl();
    }

    public function setUp(): void
    {
        Rules::setBrowserTriggerArchiving(false);
        Config::getInstance()->General['process_new_segments_from'] = 'segment_creation_time';
        Fixture::createSuperUser();

        $this->setUpWebsites();
        $this->setUpSegments();
        $this->trackVisits();
    }

    public function tearDown(): void
    {
        // empty
    }

    public function provideContainerConfig(): array
    {
        Date::$now = strtotime('2024-05-03 10:00:00');

        if (!$this->inTestEnv && !$this->inTestRequest) {
            return [];
        }

        if ($this->inTestRequest) {
            return [
                'observers.global' => DI::add([
                    [
                        'API.CoreAdminHome.archiveReports',
                        DI::value(Closure::fromCallable([$this->stepControl, 'handleAPIArchiveReports'])),
                    ],
                ]),
            ];
        }

        return [
            'ini.tests.enable_logging' => 1,
            'log.handlers' => static function (Container $c) {
                return [$c->get(EchoHandler::class)];
            },
            'observers.global' => DI::add([
                [
                    'CronArchive.alterArchivingRequestUrl',
                    DI::value(static function (&$url) {
                        $url .= '&' . self::ENV_TRIGGER . '=1';
                    }),
                ],
                [
                    'CronArchive.init.finish',
                    DI::value(Closure::fromCallable([$this->stepControl, 'handleCronArchiveStart'])),
                ],
                [
                    'ScheduledTasks.execute',
                    DI::value(Closure::fromCallable([$this->stepControl, 'handleScheduledTasksExecute'])),
                ],
                [
                    'ScheduledTasks.shouldExecuteTask',
                    DI::value(Closure::fromCallable([$this->stepControl, 'handleScheduledTasksShouldExecute'])),
                ],
            ]),
        ];
    }

    private function setUpSegments(): void
    {
        APISegmentEditor::getInstance()->add(
            self::TEST_SEGMENT_CH,
            self::TEST_SEGMENT_CH,
            $this->idSite,
            $autoArchive = true,
            $enabledAllUsers = true
        );

        APISegmentEditor::getInstance()->add(
            self::TEST_SEGMENT_FF,
            self::TEST_SEGMENT_FF,
            $this->idSite,
            $autoArchive = true,
            $enabledAllUsers = true
        );
    }

    private function setUpWebsites(): void
    {
        if (!self::siteCreated($this->idSite)) {
            self::createWebsite('2021-01-01');
        }
    }

    private function trackVisits(): void
    {
        $t = self::getTracker($this->idSite, $this->today, $defaultInit = true);
        $t->setUrl('http://example.org/index.htm');

        self::checkResponse($t->doTrackPageView('0'));
    }
}