|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Matomo - free/libre analytics platform |
| 5 | + * |
| 6 | + * @link https://matomo.org |
| 7 | + * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace Piwik\Plugins\OpenApiDocs\tests\Integration; |
| 13 | + |
| 14 | +use Piwik\Access; |
| 15 | +use Piwik\Container\StaticContainer; |
| 16 | +use Piwik\Plugin\Manager; |
| 17 | +use Piwik\Plugins\OpenApiDocs\Controller; |
| 18 | +use Piwik\Tests\Framework\Fixture; |
| 19 | +use Piwik\Tests\Framework\Mock\FakeAccess; |
| 20 | +use Piwik\Tests\Framework\TestCase\IntegrationTestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * @group OpenApiDocs |
| 24 | + * @group ControllerTest |
| 25 | + * @group Plugins |
| 26 | + */ |
| 27 | +class ControllerTest extends IntegrationTestCase |
| 28 | +{ |
| 29 | + private Controller $controller; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var array<string, mixed> |
| 33 | + */ |
| 34 | + private array $backupGet; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var array<string, mixed> |
| 38 | + */ |
| 39 | + private array $backupRequest; |
| 40 | + |
| 41 | + public function setUp(): void |
| 42 | + { |
| 43 | + parent::setUp(); |
| 44 | + |
| 45 | + $this->backupGet = $_GET; |
| 46 | + $this->backupRequest = $_REQUEST; |
| 47 | + |
| 48 | + Fixture::createSuperUser(); |
| 49 | + if (!Fixture::siteCreated(1)) { |
| 50 | + Fixture::createWebsite('2012-01-01 00:00:00'); |
| 51 | + } |
| 52 | + |
| 53 | + Fixture::resetTranslations(); |
| 54 | + Fixture::loadAllTranslations(); |
| 55 | + |
| 56 | + Manager::getInstance()->loadPlugin('OpenApiDocs'); |
| 57 | + |
| 58 | + $_GET = [ |
| 59 | + 'idSite' => 1, |
| 60 | + 'period' => 'day', |
| 61 | + 'date' => 'today', |
| 62 | + ]; |
| 63 | + $_REQUEST = $_GET; |
| 64 | + |
| 65 | + $this->controller = new Controller(); |
| 66 | + } |
| 67 | + |
| 68 | + public function tearDown(): void |
| 69 | + { |
| 70 | + $_GET = $this->backupGet; |
| 71 | + $_REQUEST = $this->backupRequest; |
| 72 | + |
| 73 | + Fixture::resetTranslations(); |
| 74 | + |
| 75 | + parent::tearDown(); |
| 76 | + } |
| 77 | + |
| 78 | + public function testSwaggerRendersAdminPageForSuperUser(): void |
| 79 | + { |
| 80 | + FakeAccess::clearAccess( |
| 81 | + $superUser = true, |
| 82 | + $idSitesAdmin = [1], |
| 83 | + $idSitesView = [1], |
| 84 | + $identity = 'superUserLogin' |
| 85 | + ); |
| 86 | + |
| 87 | + $html = $this->controller->swagger(); |
| 88 | + |
| 89 | + $this->assertNotSame('', $html); |
| 90 | + $this->assertStringContainsString('vue-entry="OpenApiDocs.SwaggerPage"', $html); |
| 91 | + $this->assertStringContainsString('Swagger', $html); |
| 92 | + } |
| 93 | + |
| 94 | + public function testSwaggerThrowsWhenUserIsNotSuperUser(): void |
| 95 | + { |
| 96 | + $originalAccess = StaticContainer::getContainer()->get(Access::class); |
| 97 | + StaticContainer::getContainer()->set(Access::class, new FakeAccess(false, [], [1], 'viewUser')); |
| 98 | + |
| 99 | + try { |
| 100 | + $this->expectException(\Exception::class); |
| 101 | + $this->expectExceptionMessage('checkUserHasSuperUserAccess'); |
| 102 | + |
| 103 | + $this->controller->swagger(); |
| 104 | + } finally { |
| 105 | + StaticContainer::getContainer()->set(Access::class, $originalAccess); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments