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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Live\tests\Integration;
use Piwik\Common;
use Piwik\Config;
use Piwik\Date;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Plugins\Live\Exception\MaxExecutionTimeExceededException;
use Piwik\Plugins\Live\Model;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tests\Framework\TestCase\SystemTestCase;
use Piwik\Tests\Integration\SegmentTest;
/**
* @group Live
* @group ModelTest
* @group Plugins
*/
class ModelTest extends IntegrationTestCase
{
public function setUp(): void
{
parent::setUp();
Fixture::createSuperUser();
$this->setSuperUser();
Fixture::createWebsite('2010-01-01');
}
public function testGetStandAndEndDateUsesNowWhenDateOutOfRange()
{
$model = new Model();
[$dateStart, $dateEnd] = $model->getStartAndEndDate($idSite = 1, 'year', (date('Y') + 1) . '-01-01');
$validDates = $this->getValidNowDates();
$this->assertTrue(in_array($dateStart->getDatetime(), $validDates));
$this->assertTrue(in_array($dateEnd->getDatetime(), $validDates));
$this->assertNotEquals($dateStart->getDatetime(), $dateEnd->getDatetime());
}
public function testGetStandAndEndDateUsesNowWhenEndDateOutOfRange()
{
$model = new Model();
[$dateStart, $dateEnd] = $model->getStartAndEndDate($idSite = 1, 'year', date('Y') . '-01-01');
$validDates = $this->getValidNowDates();
$this->assertEquals(date('Y') . '-01-01 00:00:00', $dateStart->getDatetime());
$this->assertTrue(in_array($dateEnd->getDatetime(), $validDates));
$this->assertNotEquals($dateStart->getDatetime(), $dateEnd->getDatetime());
}
private function getValidNowDates()
{
$now = Date::now();
$validDates = [$now->getDatetime()];
$validDates[] = $now->subSeconds(1)->getDatetime();
$validDates[] = $now->subSeconds(2)->getDatetime();
$validDates[] = $now->addPeriod(1, 'second')->getDatetime();
$validDates[] = $now->addPeriod(2, 'second')->getDatetime();
return $validDates;
}
public function testHandleMaxExecutionTimeErrorDoesNotThrowExceptionWhenNotExceededTime()
{
self::expectNotToPerformAssertions();
$db = Db::get();
$e = new \Exception('foo bar baz');
$sql = 'SELECT 1';
$bind = [];
$segment = '';
$dateStart = Date::now()->subDay(1);
$dateEnd = Date::now();
$minTimestamp = 1;
$limit = 50;
Model::handleMaxExecutionTimeError($db, $e, $segment, $dateStart, $dateEnd, $minTimestamp, $limit, [$sql, $bind]);
}
public function testHandleMaxExecutionTimeErrorWhenTimeIsExceededNoReasonFound()
{
$this->expectException(\Piwik\Plugins\Live\Exception\MaxExecutionTimeExceededException::class);
$this->expectExceptionMessage('Live_QueryMaxExecutionTimeExceeded Live_QueryMaxExecutionTimeExceededReasonUnknown');
$db = Db::get();
$e = new \Exception('[3024] Query execution was interrupted, maximum statement execution time exceeded');
$sql = 'SELECT 1';
$bind = array();
$segment = '';
$dateStart = Date::now()->subDay(1);
$dateEnd = Date::now();
$minTimestamp = null;
$limit = 50;
Model::handleMaxExecutionTimeError($db, $e, $segment, $dateStart, $dateEnd, $minTimestamp, $limit, [$sql, $bind]);
}
public function testHandleMaxExecutionTimeErrorWhenTimeIsExceededManyReasonsFound()
{
$this->expectException(\Piwik\Plugins\Live\Exception\MaxExecutionTimeExceededException::class);
$this->expectExceptionMessage('Live_QueryMaxExecutionTimeExceeded Live_QueryMaxExecutionTimeExceededReasonDateRange Live_QueryMaxExecutionTimeExceededReasonSegment Live_QueryMaxExecutionTimeExceededLimit');
$db = Db::get();
$e = new \Exception('Query execution was interrupted, maximum statement execution time exceeded');
$segment = 'userId>=1';
$dateStart = Date::now()->subDay(10);
$dateEnd = Date::now();
$minTimestamp = null;
$limit = 5000;
Model::handleMaxExecutionTimeError($db, $e, $segment, $dateStart, $dateEnd, $minTimestamp, $limit, ['param' => 'value']);
}
public function testGetLastMinutesCounterForQueryMaxExecutionTime()
{
if (SystemTestCase::isMysqli()) {
$this->markTestSkipped('max_execution_time not supported on mysqli');
return;
}
$this->expectException(MaxExecutionTimeExceededException::class);
$this->expectExceptionMessage('Live_QueryMaxExecutionTimeExceeded');
$this->setLowestMaxExecutionTime();
$this->trackPageView();
$model = new Model();
$model->queryAndWhereSleepTestsOnly = true;
$model->getNumVisits(1, 999999, '');
}
public function testQueryAdjacentVisitorIdMaxExecutionTime()
{
if (SystemTestCase::isMysqli()) {
$this->markTestSkipped('max_execution_time not supported on mysqli');
return;
}
$this->expectException(MaxExecutionTimeExceededException::class);
$this->expectExceptionMessage('Live_QueryMaxExecutionTimeExceeded');
$this->setLowestMaxExecutionTime();
$this->trackPageView();
$model = new Model();
$model->queryAndWhereSleepTestsOnly = true;
$model->queryAdjacentVisitorId(1, '1234567812345678', Date::yesterday()->getDatetime(), '', true);
}
public function testGetStandAndEndDate()
{
$model = new Model();
[$dateStart, $dateEnd] = $model->getStartAndEndDate($idSite = 1, 'year', '2018-02-01');
$this->assertEquals('2018-01-01 00:00:00', $dateStart->getDatetime());
$this->assertEquals('2019-01-01 00:00:00', $dateEnd->getDatetime());
}
public function testIsLookingAtMoreThanOneDayWhenNoDateSet()
{
$model = new Model();
$this->assertTrue($model->isLookingAtMoreThanOneDay(null, null, null));
}
public function testIsLookingAtMoreThanOneDayWhenNoStartDateSet()
{
$model = new Model();
$this->assertTrue($model->isLookingAtMoreThanOneDay(null, Date::now(), null));
}
public function testIsLookingAtMoreThanOneDayWhenNoStartDateSetAndMinTimestampIsOld()
{
$model = new Model();
$this->assertTrue($model->isLookingAtMoreThanOneDay(null, Date::now(), Date::now()->subDay(5)->getTimestamp()));
}
public function testIsLookingAtMoreThanOneDayWhenNoStartDateSetButMinTimestampIsRecent()
{
$model = new Model();
$this->assertFalse($model->isLookingAtMoreThanOneDay(null, Date::now(), Date::now()->subHour(5)->getTimestamp()));
}
public function testIsLookingAtMoreThanOneDayWhenNoEndDateIsSetStartDateIsOld()
{
$model = new Model();
$this->assertTrue($model->isLookingAtMoreThanOneDay(Date::now()->subDay(5), null, null));
}
public function testIsLookingAtMoreThanOneDayWhenNoEndDateIsSetStartDateIsRecent()
{
$model = new Model();
$this->assertFalse($model->isLookingAtMoreThanOneDay(Date::now()->subHour(5), null, null));
}
public function testIsLookingAtMoreThanOneDayWhenStartAndEndDateIsSetOnlyOneDay()
{
$model = new Model();
$this->assertFalse($model->isLookingAtMoreThanOneDay(Date::yesterday()->subDay(1), Date::yesterday(), null));
}
public function testIsLookingAtMoreThanOneDayWhenStartAndEndDateIsSetMoreThanOneDay()
{
$model = new Model();
$this->assertTrue($model->isLookingAtMoreThanOneDay(Date::yesterday()->subDay(2), Date::yesterday(), null));
}
public function testMakeLogVisitsQueryString()
{
$model = new Model();
[$dateStart, $dateEnd] = $model->getStartAndEndDate($idSite = 1, 'month', '2010-01-01');
[$sql, $bind] = $model->makeLogVisitsQueryString(
$idSite = 1,
$dateStart,
$dateEnd,
$segment = false,
$offset = 0,
$limit = 100,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = false
);
$expectedSql = ' SELECT log_visit.*
FROM ' . Common::prefixTable('log_visit') . ' AS log_visit
WHERE log_visit.idsite in (?)
AND log_visit.visit_last_action_time >= ?
AND log_visit.visit_last_action_time <= ?
ORDER BY log_visit.idsite DESC, log_visit.visit_last_action_time DESC, log_visit.idvisit DESC
LIMIT 0, 100';
$expectedBind = array(
'1',
'2010-01-01 00:00:00',
'2010-02-01 00:00:00',
);
$this->assertEquals(SegmentTest::removeExtraWhiteSpaces($expectedSql), SegmentTest::removeExtraWhiteSpaces($sql));
$this->assertEquals(SegmentTest::removeExtraWhiteSpaces($expectedBind), SegmentTest::removeExtraWhiteSpaces($bind));
}
public function testMakeLogVisitsQueryStringWithMultipleIdSites()
{
Piwik::addAction('Live.API.getIdSitesString', function (&$idSites) {
$idSites = array(2,3,4);
});
$model = new Model();
[$dateStart, $dateEnd] = $model->getStartAndEndDate($idSite = 1, 'month', '2010-01-01');
[$sql, $bind] = $model->makeLogVisitsQueryString(
$idSite = 1,
$dateStart,
$dateEnd,
$segment = false,
$offset = 0,
$limit = 100,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = false
);
$expectedSql = ' SELECT log_visit.*
FROM ' . Common::prefixTable('log_visit') . ' AS log_visit
WHERE log_visit.idsite in (?,?,?)
AND log_visit.visit_last_action_time >= ?
AND log_visit.visit_last_action_time <= ?
ORDER BY log_visit.visit_last_action_time DESC, log_visit.idvisit DESC
LIMIT 0, 100';
$expectedBind = array(
'2',
'3',
'4',
'2010-01-01 00:00:00',
'2010-02-01 00:00:00',
);
$this->assertEquals(SegmentTest::removeExtraWhiteSpaces($expectedSql), SegmentTest::removeExtraWhiteSpaces($sql));
$this->assertEquals(SegmentTest::removeExtraWhiteSpaces($expectedBind), SegmentTest::removeExtraWhiteSpaces($bind));
}
public function testMakeLogVisitsQueryStringWithOffset()
{
$model = new Model();
[$dateStart, $dateEnd] = $model->getStartAndEndDate($idSite = 1, 'month', '2010-01-01');
[$sql, $bind] = $model->makeLogVisitsQueryString(
$idSite = 1,
$dateStart,
$dateEnd,
$segment = false,
$offset = 15,
$limit = 100,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = false
);
$expectedSql = ' SELECT log_visit.*
FROM ' . Common::prefixTable('log_visit') . ' AS log_visit
WHERE log_visit.idsite in (?)
AND log_visit.visit_last_action_time >= ?
AND log_visit.visit_last_action_time <= ?
ORDER BY log_visit.idsite DESC, log_visit.visit_last_action_time DESC, log_visit.idvisit DESC
LIMIT 15, 100';
$expectedBind = array(
'1',
'2010-01-01 00:00:00',
'2010-02-01 00:00:00',
);
$this->assertEquals(SegmentTest::removeExtraWhiteSpaces($expectedSql), SegmentTest::removeExtraWhiteSpaces($sql));
$this->assertEquals(SegmentTest::removeExtraWhiteSpaces($expectedBind), SegmentTest::removeExtraWhiteSpaces($bind));
}
public function testMakeLogVisitsQueryStringWhenSegment()
{
$model = new Model();
[$dateStart, $dateEnd] = $model->getStartAndEndDate($idSite = 1, 'month', '2010-01-01');
[$sql, $bind] = $model->makeLogVisitsQueryString(
$idSite = 1,
$dateStart,
$dateEnd,
$segment = 'siteSearchCategory==Test',
$offset = 10,
$limit = 100,
$visitorId = 'abc',
$minTimestamp = false,
$filterSortOrder = false
);
$expectedSql = ' SELECT log_visit.*
FROM log_visit AS log_visit
LEFT JOIN log_link_visit_action AS log_link_visit_action ON log_link_visit_action.idvisit = log_visit.idvisit
WHERE (
log_visit.idsite in (?)
AND log_visit.idvisitor = ?
AND log_visit.visit_last_action_time >= ?
AND log_visit.visit_last_action_time <= ? )
AND ( log_link_visit_action.search_cat = ? )
GROUP BY log_visit.idvisit
ORDER BY log_visit.idsite DESC, log_visit.visit_last_action_time DESC, log_visit.idvisit DESC
LIMIT 10, 100';
$expectedBind = array(
'1',
Common::hex2bin('abc'),
'2010-01-01 00:00:00',
'2010-02-01 00:00:00',
'Test',
);
$this->assertEquals(SegmentTest::removeExtraWhiteSpaces($expectedSql), SegmentTest::removeExtraWhiteSpaces($sql));
$this->assertEquals(SegmentTest::removeExtraWhiteSpaces($expectedBind), SegmentTest::removeExtraWhiteSpaces($bind));
}
public function testMakeLogVisitsQueryStringAddsMaxExecutionHintIfConfigured()
{
$this->setMaxExecutionTime(30);
Config\DatabaseConfig::setConfigValue('schema', 'Mysql');
Db\Schema::unsetInstance();
$model = new Model();
[$dateStart, $dateEnd] = $model->getStartAndEndDate($idSite = 1, 'month', '2010-01-01');
[$sql, $bind] = $model->makeLogVisitsQueryString(
$idSite = 1,
$dateStart,
$dateEnd,
$segment = false,
$offset = 0,
$limit = 100,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = false
);
$expectedSql = 'SELECT /*+ MAX_EXECUTION_TIME(30000) */
log_visit.*';
$this->setMaxExecutionTime(-1);
$this->assertStringStartsWith($expectedSql, trim($sql));
}
public function testMakeLogVisitsQueryStringDoesNotAddsMaxExecutionHintForVisitorIds()
{
$this->setMaxExecutionTime(30);
$model = new Model();
[$dateStart, $dateEnd] = $model->getStartAndEndDate($idSite = 1, 'month', '2010-01-01');
[$sql, $bind] = $model->makeLogVisitsQueryString(
$idSite = 1,
$dateStart,
$dateEnd,
$segment = false,
$offset = 0,
$limit = 100,
$visitorId = '1234567812345678',
$minTimestamp = false,
$filterSortOrder = false
);
$expectedSql = 'SELECT
log_visit.*';
$this->setMaxExecutionTime(-1);
$this->assertStringStartsWith($expectedSql, trim($sql));
}
public function testSplitDatesIntoMultipleQueriesNotMoreThanADayUsesOnlyOneQuery()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2010-01-02 00:00:00', $limit = 5, $offset = 0);
$this->assertEquals(array('2010-01-01 00:00:00 2010-01-02 00:00:00'), $dates);
}
public function testSplitDatesIntoMultipleQueriesNotMoreThanADayUsesOnlyOneQueryDesc()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2010-01-02 00:00:00', $limit = 5, $offset = 0, 'asc');
$this->assertEquals(array('2010-01-01 00:00:00 2010-01-02 00:00:00'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanADayLessThanAWeek()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2010-01-02 00:01:00', $limit = 5, $offset = 0);
$this->assertEquals(array('2010-01-01 00:01:00 2010-01-02 00:01:00', '2010-01-01 00:00:00 2010-01-01 00:00:59'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanADayLessThanAWeekAsc()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2010-01-02 00:01:00', $limit = 5, $offset = 0, 'asc');
$this->assertEquals(array('2010-01-01 00:00:00 2010-01-01 23:59:59', '2010-01-02 00:00:00 2010-01-02 00:01:00'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanAWeekLessThanMonth()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2010-01-20 04:01:00', $limit = 5, $offset = 0);
$this->assertEquals(array('2010-01-19 04:01:00 2010-01-20 04:01:00', '2010-01-12 04:01:00 2010-01-19 04:00:59', '2010-01-01 00:00:00 2010-01-12 04:00:59'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanAWeekLessThanMonthAsc()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2010-01-20 04:01:00', $limit = 5, $offset = 0, 'asc');
$this->assertEquals(array('2010-01-01 00:00:00 2010-01-01 23:59:59', '2010-01-02 00:00:00 2010-01-08 23:59:59', '2010-01-09 00:00:00 2010-01-20 04:01:00'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanMonthLessThanYear()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2010-02-20 04:01:00', $limit = 5, $offset = 0);
$this->assertEquals(array('2010-02-19 04:01:00 2010-02-20 04:01:00', '2010-02-12 04:01:00 2010-02-19 04:00:59', '2010-01-13 04:01:00 2010-02-12 04:00:59', '2010-01-01 00:00:00 2010-01-13 04:00:59'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanMonthLessThanYearAsc()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2010-02-20 04:01:00', $limit = 5, $offset = 0, 'asc');
$this->assertEquals(array('2010-01-01 00:00:00 2010-01-01 23:59:59', '2010-01-02 00:00:00 2010-01-08 23:59:59', '2010-01-09 00:00:00 2010-02-07 23:59:59', '2010-02-08 00:00:00 2010-02-20 04:01:00'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanYear()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2012-02-20 04:01:00', $limit = 5, $offset = 0);
$this->assertEquals(array('2012-02-19 04:01:00 2012-02-20 04:01:00', '2012-02-12 04:01:00 2012-02-19 04:00:59', '2012-01-13 04:01:00 2012-02-12 04:00:59', '2011-01-01 04:01:00 2012-01-13 04:00:59', '2010-01-01 00:00:00 2011-01-01 04:00:59'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanYearAsc()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2012-02-20 04:01:00', $limit = 5, $offset = 0, 'asc');
$this->assertEquals(array('2010-01-01 00:00:00 2010-01-01 23:59:59', '2010-01-02 00:00:00 2010-01-08 23:59:59', '2010-01-09 00:00:00 2010-02-07 23:59:59', '2010-02-08 00:00:00 2011-02-07 23:59:59', '2011-02-08 00:00:00 2012-02-20 04:01:00'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanYearWithOffsetUsesLessQueries()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2012-02-20 04:01:00', $limit = 5, $offset = 5);
$this->assertEquals(array('2012-02-19 04:01:00 2012-02-20 04:01:00', '2012-02-12 04:01:00 2012-02-19 04:00:59', '2010-01-01 00:00:00 2012-02-12 04:00:59'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanYearWithOffsetUsesLessQueriesAsc()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 04:01:00', '2012-02-20 00:00:00', $limit = 5, $offset = 5, 'asc');
$this->assertEquals(array('2010-01-01 04:01:00 2010-01-02 04:00:59', '2010-01-02 04:01:00 2010-01-09 04:00:59', '2010-01-09 04:01:00 2012-02-20 00:00:00'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanYearNoLimitDoesntUseMultipleQueries()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 00:00:00', '2012-02-20 04:01:00', $limit = 0, $offset = 0);
$this->assertEquals(array('2010-01-01 00:00:00 2012-02-20 04:01:00'), $dates);
}
public function testSplitDatesIntoMultipleQueriesMoreThanYearNoLimitDoesntUseMultipleQueriesAsc()
{
$dates = $this->splitDatesIntoMultipleQueries('2010-01-01 04:01:00', '2012-02-20 00:00:00', $limit = 0, $offset = 0, 'asc');
$this->assertEquals(array('2010-01-01 04:01:00 2012-02-20 00:00:00'), $dates);
}
public function testSplitDatesIntoMultipleQueriesNoStartDate()
{
$dates = $this->splitDatesIntoMultipleQueries(false, '2012-02-20 04:01:00', $limit = 5, $offset = 0);
$this->assertEquals(array('2012-02-19 04:01:00 2012-02-20 04:01:00', '2012-02-12 04:01:00 2012-02-19 04:00:59', '2012-01-13 04:01:00 2012-02-12 04:00:59', '2011-01-01 04:01:00 2012-01-13 04:00:59', ' 2011-01-01 04:00:59'), $dates);
}
private function splitDatesIntoMultipleQueries($startDate, $endDate, $limit, $offset, $order = 'desc')
{
if ($startDate) {
$startDate = Date::factory($startDate);
}
if ($endDate) {
$endDate = Date::factory($endDate);
}
$model = new Model();
$queries = $model->splitDatesIntoMultipleQueries($startDate, $endDate, $limit, $offset, $order);
return array_map(function ($query) {
return ($query[0] ? $query[0]->getDatetime() : '') . ' ' . ($query[1] ? $query[1]->getDatetime() : '');
}, $queries);
}
public function testQueryLogVisitsSkipsOffsetAcrossSplitDateRanges()
{
// build visits across 3 days so the query will be split into multiple ranges
$this->trackVisitAtTime('2010-01-01 03:00:00');
$this->trackVisitAtTime('2010-01-01 06:10:00');
$this->trackVisitAtTime('2010-01-01 09:20:00');
$this->trackVisitAtTime('2010-01-02 03:00:00');
$this->trackVisitAtTime('2010-01-02 06:10:00');
$this->trackVisitAtTime('2010-01-03 03:00:00');
$this->trackVisitAtTime('2010-01-03 06:10:00');
$this->trackVisitAtTime('2010-01-03 09:20:00');
$this->assertEquals(8, $this->countVisitsBetween('2010-01-01 00:00:00', '2010-01-04 00:00:00'));
$model = new Model();
// sanity check: without offset we see all visits
$allVisits = $model->queryLogVisits(
1,
'range',
'2010-01-01,2010-01-03',
$segment = '',
$offset = 0,
$limit = 10,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = 'desc'
);
$this->assertCount(8, $allVisits);
$visits = $model->queryLogVisits(
1,
'range',
'2010-01-01,2010-01-03',
$segment = '',
$offset = 6,
$limit = 2,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = 'desc'
);
$this->assertCount(2, $visits);
$this->assertEquals('2010-01-01 06:10:00', $visits[0]['visit_last_action_time']);
$this->assertEquals('2010-01-01 03:00:00', $visits[1]['visit_last_action_time']);
}
public function testQueryLogVisitsOffsetAcrossSplitDateRangesAscending()
{
$this->trackVisitAtTime('2010-02-01 03:00:00');
$this->trackVisitAtTime('2010-02-01 06:10:00');
$this->trackVisitAtTime('2010-02-01 09:20:00');
$this->trackVisitAtTime('2010-02-02 03:00:00');
$this->trackVisitAtTime('2010-02-02 06:10:00');
$this->trackVisitAtTime('2010-02-03 03:00:00');
$this->trackVisitAtTime('2010-02-03 06:10:00');
$this->trackVisitAtTime('2010-02-03 09:20:00');
$this->assertEquals(8, $this->countVisitsBetween('2010-02-01 00:00:00', '2010-02-04 00:00:00'));
$model = new Model();
$visits = $model->queryLogVisits(
1,
'range',
'2010-02-01,2010-02-03',
$segment = '',
$offset = 6,
$limit = 2,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = 'asc'
);
$this->assertCount(2, $visits);
$this->assertEquals('2010-02-03 06:10:00', $visits[0]['visit_last_action_time']);
$this->assertEquals('2010-02-03 09:20:00', $visits[1]['visit_last_action_time']);
}
public function testQueryLogVisitsOffsetAcrossYearRange()
{
$dates = [
'2010-01-01 03:00:00',
'2010-02-01 03:00:00',
'2010-03-01 03:00:00',
'2010-04-01 03:00:00',
'2010-05-01 03:00:00',
'2010-06-01 03:00:00',
'2010-07-01 03:00:00',
'2010-08-01 03:00:00',
'2010-09-01 03:00:00',
'2010-10-01 03:00:00',
'2010-11-01 03:00:00',
'2010-12-01 03:00:00',
'2011-01-01 03:00:00',
];
foreach ($dates as $dateTime) {
$this->trackVisitAtTime($dateTime);
}
$this->assertEquals(13, $this->countVisitsBetween('2010-01-01 00:00:00', '2011-02-01 00:00:00'));
$model = new Model();
$visits = $model->queryLogVisits(
1,
'range',
'2010-01-01,2011-01-01',
$segment = '',
$offset = 10,
$limit = 2,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = 'desc'
);
$this->assertCount(2, $visits);
$this->assertEquals('2010-03-01 03:00:00', $visits[0]['visit_last_action_time']);
$this->assertEquals('2010-02-01 03:00:00', $visits[1]['visit_last_action_time']);
}
public function testQueryLogVisitsOffsetBeyondTotalReturnsEmpty()
{
$this->trackVisitAtTime('2010-04-01 03:00:00');
$this->trackVisitAtTime('2010-04-01 06:10:00');
$this->trackVisitAtTime('2010-04-02 03:00:00');
$this->assertEquals(3, $this->countVisitsBetween('2010-04-01 00:00:00', '2010-04-03 00:00:00'));
$model = new Model();
$visits = $model->queryLogVisits(
1,
'range',
'2010-04-01,2010-04-02',
$segment = '',
$offset = 10,
$limit = 5,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = 'desc'
);
$this->assertSame([], $visits);
}
public function testQueryLogVisitsOffsetWeekPeriod()
{
$this->trackVisitAtTime('2010-01-05 01:00:00');
$this->trackVisitAtTime('2010-01-06 01:00:00');
$this->trackVisitAtTime('2010-01-07 01:00:00');
$this->assertEquals(3, $this->countVisitsBetween('2010-01-04 00:00:00', '2010-01-08 00:00:00'));
$model = new Model();
$visits = $model->queryLogVisits(
1,
'week',
'2010-01-05',
$segment = '',
$offset = 1,
$limit = 1,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = 'desc'
);
$this->assertCount(1, $visits);
$this->assertEquals('2010-01-06 01:00:00', $visits[0]['visit_last_action_time']);
}
public function testQueryLogVisitsOffsetMonthPeriod()
{
$this->trackVisitAtTime('2010-06-05 01:00:00');
$this->trackVisitAtTime('2010-06-10 01:00:00');
$this->trackVisitAtTime('2010-06-20 01:00:00');
$this->trackVisitAtTime('2010-06-25 01:00:00');
$this->assertEquals(4, $this->countVisitsBetween('2010-06-01 00:00:00', '2010-07-01 00:00:00'));
$model = new Model();
$visits = $model->queryLogVisits(
1,
'month',
'2010-06-15',
$segment = '',
$offset = 2,
$limit = 1,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = 'desc'
);
$this->assertCount(1, $visits);
$this->assertEquals('2010-06-10 01:00:00', $visits[0]['visit_last_action_time']);
}
public function testQueryLogVisitsOffsetYearPeriod()
{
$this->trackVisitAtTime('2011-01-01 03:00:00');
$this->trackVisitAtTime('2011-03-01 03:00:00');
$this->trackVisitAtTime('2011-06-01 03:00:00');
$this->trackVisitAtTime('2011-09-01 03:00:00');
$this->trackVisitAtTime('2011-12-01 03:00:00');
$this->assertEquals(5, $this->countVisitsBetween('2011-01-01 00:00:00', '2012-01-01 00:00:00'));
$model = new Model();
$visits = $model->queryLogVisits(
1,
'year',
'2011-01-01',
$segment = '',
$offset = 3,
$limit = 2,
$visitorId = false,
$minTimestamp = false,
$filterSortOrder = 'desc'
);
$this->assertCount(2, $visits);
$this->assertEquals('2011-03-01 03:00:00', $visits[0]['visit_last_action_time']);
$this->assertEquals('2011-01-01 03:00:00', $visits[1]['visit_last_action_time']);
}
protected function setSuperUser()
{
FakeAccess::$superUser = true;
}
public function provideContainerConfig()
{
return array(
'Piwik\Access' => new FakeAccess(),
);
}
private function setLowestMaxExecutionTime(): void
{
$this->setMaxExecutionTime(0.001);
}
private function setMaxExecutionTime($time): void
{
$config = Config::getInstance();
$general = $config->General;
$general['live_query_max_execution_time'] = $time;
$config->General = $general;
}
private function trackPageView(): void
{
// Needed for the tests that may execute a sleep() to test max execution time. Otherwise if the table is empty
// the sleep would not be executed making the tests fail randomly
$t = Fixture::getTracker(1, Date::now()->getDatetime(), $defaultInit = true);
$t->setTokenAuth(Fixture::getTokenAuth());
$t->setVisitorId(substr(sha1('X4F66G776HGI'), 0, 16));
$t->doTrackPageView('foo');
}
private function trackVisitAtTime(string $dateTime): void
{
$t = Fixture::getTracker(1, $dateTime, $defaultInit = true);
$t->setTokenAuth(Fixture::getTokenAuth());
$t->setNewVisitorId();
$t->setForceVisitDateTime($dateTime);
$t->setUrl('http://example.org/' . str_replace([' ', ':'], '-', $dateTime));
Fixture::checkResponse($t->doTrackPageView('Visit at ' . $dateTime));
}
private function countVisitsBetween(string $startDate, string $endDate): int
{
return (int) Db::fetchOne(
'SELECT COUNT(*) FROM ' . Common::prefixTable('log_visit') . ' WHERE visit_last_action_time >= ? AND visit_last_action_time <= ? AND idsite = ?',
[$startDate, $endDate, 1]
);
}
}
|