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

ChunksTest.php « Archive « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bd8621b8e81a67146be965b97f9bfd6e35cf898a (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
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
<?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\Tests\Integration\Archive;

use Piwik\Archive;
use Piwik\ArchiveProcessor;
use Piwik\ArchiveProcessor\Parameters;
use Piwik\DataAccess\ArchiveTableCreator;
use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Date;
use Piwik\Db;
use Piwik\Segment;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\Mock\Site;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Period\Factory as PeriodFactory;


/**
 * @group ChunksTest
 * @group Chunks
 * @group Core
 */
class ChunksTest extends IntegrationTestCase
{
    private $date = '2015-01-01';

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

        // setup the access layer
        FakeAccess::$superUser = true;

        Fixture::createWebsite('2015-01-01 00:00:00');
    }

    public function test_subtables_willBeSplitIntoChunks()
    {
        $numSubtablesToGenerate = 1053;

        $blobs = $this->generateDataTableWithManySubtables($numSubtablesToGenerate);
        $this->assertCount($numSubtablesToGenerate + 1, $blobs); // +1 for the root table

        $recordName = 'Actions_MyRecord';
        $archiver = $this->createPluginsArchiver();
        $archiver->archiveProcessor->insertBlobRecord($recordName, $blobs);
        $archiver->finalizeArchive();

        // verify they were split into chunks
        $archiveRows = $this->getAllRowsFromArchiveBlobTable('name');
        $expectedArchiveNames = array(
            $recordName,
            $recordName. '_chunk_0_99',
            $recordName. '_chunk_1000_1099',
            $recordName. '_chunk_100_199',
            $recordName. '_chunk_200_299',
            $recordName. '_chunk_300_399',
            $recordName. '_chunk_400_499',
            $recordName. '_chunk_500_599',
            $recordName. '_chunk_600_699',
            $recordName. '_chunk_700_799',
            $recordName. '_chunk_800_899',
            $recordName. '_chunk_900_999',
        );

        $this->assertEquals($expectedArchiveNames, array_column($archiveRows, 'name'));

        // verify all have same archiveIds
        $expectedArchiveIds = array_fill(0, count($expectedArchiveNames), $archiveId = '1');
        $this->assertEquals($expectedArchiveIds, array_column($archiveRows, 'idarchive'));

        // verify the subtables were actually splitted into chunks
        foreach ($archiveRows as $row) {
            $value = unserialize(gzuncompress($row['value']));
            $this->assertTrue(is_array($value));
            if ($row['name'] == $recordName) {
                $this->assertCount($numSubtablesToGenerate, $value); // 1053 rows
            } elseif ($row['name'] == $recordName . '_chunk_1000_1099') {
                $this->assertCount(($numSubtablesToGenerate % Archive\Chunk::NUM_TABLES_IN_CHUNK) + 1, $value); // 53 subtables
            } elseif ($row['name'] == $recordName . '_chunk_0_99') {
                $this->assertCount(Archive\Chunk::NUM_TABLES_IN_CHUNK - 1, $value); // one less as we do not store the root table here
            } else {
                $this->assertCount(Archive\Chunk::NUM_TABLES_IN_CHUNK, $value);
            }
        }

        // should be able to rebuild the datatable
        $archive = Archive::build(1, 'day', $this->date);
        $table = $archive->getDataTable($recordName);
        $this->assertSame(1053, $table->getRowsCount());
        $this->assertSame('Label Test 1', $table->getFirstRow()->getColumn('label'));
        $this->assertSame(1, $table->getFirstRow()->getColumn('nb_visits'));
    }

    private function getAllRowsFromArchiveBlobTable()
    {
        $table = ArchiveTableCreator::getBlobTable(Date::factory($this->date));
        $rows  = Db::fetchAll("SELECT * FROM " . $table);

        return $rows;
    }

    private function generateDataTableWithManySubtables($numSubtables)
    {
        $dataTable = new DataTable();

        for ($i = 1; $i <= $numSubtables; $i++) {
            $row = new Row(array(Row::COLUMNS => array('label' => 'Label Test ' . $i, 'nb_visits' => $i)));

            $subtable = DataTable::makeFromSimpleArray(array(array('label' => 'subtable' . $i, 'nb_visits' => $i)));
            $row->setSubtable($subtable);

            $dataTable->addRow($row);
        }

        return $dataTable->getSerialized();
    }

    private function createArchiveProcessorParamaters()
    {
        $oPeriod = PeriodFactory::makePeriodFromQueryParams('UTC', 'day', $this->date);

        $segment = new Segment(false, array(1));
        $params  = new Parameters(new Site(1), $oPeriod, $segment);

        return $params;
    }

    private function createPluginsArchiver()
    {
        $params = $this->createArchiveProcessorParamaters();

        return new ArchiveProcessor\PluginsArchiver($params);
    }

    public function provideContainerConfig()
    {
        return array(
            'Piwik\Access' => new FakeAccess()
        );
    }
}