|
10 | 10 | namespace Piwik\Tests\Integration\Tracker; |
11 | 11 |
|
12 | 12 | use Piwik\Date; |
| 13 | +use Piwik\Exception\InvalidRequestParameterException; |
13 | 14 | use Piwik\Exception\UnexpectedWebsiteFoundException; |
14 | 15 | use Piwik\Tests\Framework\Fixture; |
15 | 16 | use Piwik\Tests\Framework\TestCase\IntegrationTestCase; |
16 | 17 | use Piwik\Tracker\Failures; |
17 | 18 | use Piwik\Tracker\Request; |
| 19 | +use Piwik\Tracker\Visit; |
18 | 20 |
|
19 | 21 | /** |
20 | 22 | * @group Failures |
@@ -301,6 +303,112 @@ public function testRemoveFailuresOlderThanDays() |
301 | 303 | ), $summary); |
302 | 304 | } |
303 | 305 |
|
| 306 | + |
| 307 | + /** |
| 308 | + * @dataProvider getInvalidSiteIds |
| 309 | + */ |
| 310 | + public function testProvidingInvalidSiteIdForTrackingDoesLogFailure(string $idsite) |
| 311 | + { |
| 312 | + try { |
| 313 | + $request = new Request(['idsite' => $idsite, 'rec' => '1', 'url' => 'https://matomo.org/index']); |
| 314 | + $visit = new Visit(); |
| 315 | + $visit->setRequest($request); |
| 316 | + $visit->handle(); |
| 317 | + self::fail('expected exception not raised'); |
| 318 | + } catch (UnexpectedWebsiteFoundException $e) { |
| 319 | + // ignore, as we expect a UnexpectedWebsiteFoundException to be thrown |
| 320 | + } |
| 321 | + |
| 322 | + self::assertCount(1, $this->failures->getAllFailures()); |
| 323 | + } |
| 324 | + |
| 325 | + public function getInvalidSiteIds(): array |
| 326 | + { |
| 327 | + return [ |
| 328 | + ['4'], |
| 329 | + ['0'], |
| 330 | + ['1234'], |
| 331 | + ]; |
| 332 | + } |
| 333 | + |
| 334 | + /** |
| 335 | + * @dataProvider getMalFormedSiteIds |
| 336 | + */ |
| 337 | + public function testProvidingMalformedSiteIdForTrackingDoesNotLogFailure(string $idsite) |
| 338 | + { |
| 339 | + try { |
| 340 | + $request = new Request(['idsite' => $idsite, 'rec' => '1', 'url' => 'https://matomo.org/index']); |
| 341 | + $visit = new Visit(); |
| 342 | + $visit->setRequest($request); |
| 343 | + $visit->handle(); |
| 344 | + self::fail('expected exception not raised'); |
| 345 | + } catch (UnexpectedWebsiteFoundException $e) { |
| 346 | + // ignore, as we expect it to be thrown |
| 347 | + } |
| 348 | + |
| 349 | + self::assertCount(0, $this->failures->getAllFailures()); |
| 350 | + } |
| 351 | + |
| 352 | + public function getMalFormedSiteIds(): array |
| 353 | + { |
| 354 | + return [ |
| 355 | + [''], |
| 356 | + ['-4'], |
| 357 | + ['1"; DROP TABLE'], |
| 358 | + ['5,6'], |
| 359 | + ['nan'], |
| 360 | + ['test5'], |
| 361 | + ]; |
| 362 | + } |
| 363 | + |
| 364 | + public function testProvidingInvalidTokenAuthForTrackingDoesLogFailure() |
| 365 | + { |
| 366 | + try { |
| 367 | + $request = new Request(['idsite' => '1', 'rec' => '1', 'url' => 'https://matomo.org/index', 'city' => 'Berlin'], '1d34ghdrg6j33uersadfg34defg342vs'); |
| 368 | + $visit = new Visit(); |
| 369 | + $visit->setRequest($request); |
| 370 | + $visit->handle(); |
| 371 | + self::fail('expected exception not raised'); |
| 372 | + } catch (InvalidRequestParameterException $e) { |
| 373 | + // ignore, as we expect that exception |
| 374 | + } |
| 375 | + |
| 376 | + self::assertCount(1, $this->failures->getAllFailures()); |
| 377 | + } |
| 378 | + |
| 379 | + /** |
| 380 | + * @dataProvider getMalFormedTokenAuths |
| 381 | + */ |
| 382 | + public function testProvidingMalformedTokenAuthForTrackingDoesNotLogFailure(string $tokenAuth) |
| 383 | + { |
| 384 | + try { |
| 385 | + $request = new Request(['idsite' => '1', 'rec' => '1', 'url' => 'https://matomo.org/index', 'city' => 'Berlin'], $tokenAuth); |
| 386 | + $visit = new Visit(); |
| 387 | + $visit->setRequest($request); |
| 388 | + $visit->handle(); |
| 389 | + self::fail('expected exception not raised'); |
| 390 | + } catch (InvalidRequestParameterException $e) { |
| 391 | + // ignore, as we expect that exception |
| 392 | + } |
| 393 | + |
| 394 | + self::assertCount(0, $this->failures->getAllFailures()); |
| 395 | + } |
| 396 | + |
| 397 | + public function getMalFormedTokenAuths(): iterable |
| 398 | + { |
| 399 | + yield 'too short token' => [ |
| 400 | + 'abc', |
| 401 | + ]; |
| 402 | + |
| 403 | + yield 'too long token' => [ |
| 404 | + 'abc2435tgadetb356z2wq3er4gbrnz367634rahne735e5wqergwert245hw45h3gq45h', |
| 405 | + ]; |
| 406 | + |
| 407 | + yield 'token with invalid chars' => [ |
| 408 | + '33dc3f!536d302$974cccb4b4d2-98f4', |
| 409 | + ]; |
| 410 | + } |
| 411 | + |
304 | 412 | private function getFailureSummary() |
305 | 413 | { |
306 | 414 | $failures = $this->failures->getAllFailures(); |
|
0 commit comments