|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cms\Tests\Controllers; |
| 4 | + |
| 5 | +use Backend\Tests\Fixtures\Models\UserFixture; |
| 6 | +use Cms\Controllers\Index; |
| 7 | +use System\Tests\Bootstrap\PluginTestCase; |
| 8 | +use Winter\Storm\Auth\AuthorizationException; |
| 9 | + |
| 10 | +class IndexPermissionTest extends PluginTestCase |
| 11 | +{ |
| 12 | + protected Index $controller; |
| 13 | + |
| 14 | + protected static array $allTypes = ['page', 'partial', 'layout', 'content', 'asset']; |
| 15 | + |
| 16 | + protected static array $permissionMap = [ |
| 17 | + 'page' => 'cms.manage_pages', |
| 18 | + 'partial' => 'cms.manage_partials', |
| 19 | + 'layout' => 'cms.manage_layouts', |
| 20 | + 'content' => 'cms.manage_content', |
| 21 | + 'asset' => 'cms.manage_assets', |
| 22 | + ]; |
| 23 | + |
| 24 | + // |
| 25 | + // getRelevantPermissionForType mapping |
| 26 | + // |
| 27 | + |
| 28 | + /** |
| 29 | + * @dataProvider permissionMappingProvider |
| 30 | + */ |
| 31 | + public function testGetRelevantPermissionForType(string $type, string $expectedPermission): void |
| 32 | + { |
| 33 | + $this->actingAs((new UserFixture)->asSuperUser()); |
| 34 | + $controller = new Index; |
| 35 | + |
| 36 | + $result = self::callProtectedMethod($controller, 'getRelevantPermissionForType', [$type]); |
| 37 | + $this->assertEquals($expectedPermission, $result); |
| 38 | + } |
| 39 | + |
| 40 | + public static function permissionMappingProvider(): array |
| 41 | + { |
| 42 | + return [ |
| 43 | + 'page maps to cms.manage_pages' => ['page', 'cms.manage_pages'], |
| 44 | + 'partial maps to cms.manage_partials' => ['partial', 'cms.manage_partials'], |
| 45 | + 'layout maps to cms.manage_layouts' => ['layout', 'cms.manage_layouts'], |
| 46 | + 'content maps to cms.manage_content' => ['content', 'cms.manage_content'], |
| 47 | + 'asset maps to cms.manage_assets' => ['asset', 'cms.manage_assets'], |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + // |
| 52 | + // validateRequestType — allowed access |
| 53 | + // |
| 54 | + |
| 55 | + /** |
| 56 | + * @dataProvider allowedAccessProvider |
| 57 | + */ |
| 58 | + public function testValidateRequestTypeAllowed(string $type, array $permissions): void |
| 59 | + { |
| 60 | + $user = new UserFixture; |
| 61 | + foreach ($permissions as $permission) { |
| 62 | + $user->withPermission($permission, true); |
| 63 | + } |
| 64 | + $this->actingAs($user); |
| 65 | + $controller = new Index; |
| 66 | + |
| 67 | + // Should not throw |
| 68 | + self::callProtectedMethod($controller, 'validateRequestType', [$type]); |
| 69 | + $this->assertTrue(true); |
| 70 | + } |
| 71 | + |
| 72 | + public static function allowedAccessProvider(): array |
| 73 | + { |
| 74 | + return [ |
| 75 | + 'pages user can access page' => ['page', ['cms.manage_pages']], |
| 76 | + 'partials user can access partial' => ['partial', ['cms.manage_partials']], |
| 77 | + 'layouts user can access layout' => ['layout', ['cms.manage_layouts']], |
| 78 | + 'content user can access content' => ['content', ['cms.manage_content']], |
| 79 | + 'assets user can access asset' => ['asset', ['cms.manage_assets']], |
| 80 | + 'all permissions can access page' => ['page', [ |
| 81 | + 'cms.manage_pages', 'cms.manage_partials', 'cms.manage_layouts', |
| 82 | + 'cms.manage_content', 'cms.manage_assets', |
| 83 | + ]], |
| 84 | + 'all permissions can access asset' => ['asset', [ |
| 85 | + 'cms.manage_pages', 'cms.manage_partials', 'cms.manage_layouts', |
| 86 | + 'cms.manage_content', 'cms.manage_assets', |
| 87 | + ]], |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + public function testSuperuserCanAccessAllTypes(): void |
| 92 | + { |
| 93 | + $this->actingAs((new UserFixture)->asSuperUser()); |
| 94 | + $controller = new Index; |
| 95 | + |
| 96 | + foreach (self::$allTypes as $type) { |
| 97 | + self::callProtectedMethod($controller, 'validateRequestType', [$type]); |
| 98 | + } |
| 99 | + $this->assertTrue(true); |
| 100 | + } |
| 101 | + |
| 102 | + // |
| 103 | + // validateRequestType — denied access |
| 104 | + // |
| 105 | + |
| 106 | + /** |
| 107 | + * @dataProvider deniedAccessProvider |
| 108 | + */ |
| 109 | + public function testValidateRequestTypeDenied(string $grantedPermission, string $deniedType): void |
| 110 | + { |
| 111 | + $user = (new UserFixture)->withPermission($grantedPermission, true); |
| 112 | + $this->actingAs($user); |
| 113 | + $controller = new Index; |
| 114 | + |
| 115 | + $this->expectException(AuthorizationException::class); |
| 116 | + self::callProtectedMethod($controller, 'validateRequestType', [$deniedType]); |
| 117 | + } |
| 118 | + |
| 119 | + public static function deniedAccessProvider(): array |
| 120 | + { |
| 121 | + $cases = []; |
| 122 | + foreach (self::$permissionMap as $grantedType => $grantedPermission) { |
| 123 | + foreach (self::$allTypes as $targetType) { |
| 124 | + if ($targetType === $grantedType) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + $cases["$grantedType user denied $targetType"] = [$grantedPermission, $targetType]; |
| 128 | + } |
| 129 | + } |
| 130 | + return $cases; |
| 131 | + } |
| 132 | + |
| 133 | + // |
| 134 | + // Constructor widget registration |
| 135 | + // |
| 136 | + |
| 137 | + public function testSuperuserGetsAllWidgets(): void |
| 138 | + { |
| 139 | + $this->actingAs((new UserFixture)->asSuperUser()); |
| 140 | + $controller = new Index; |
| 141 | + |
| 142 | + $this->assertNotNull($controller->widget->pageList ?? null, 'pageList should be registered'); |
| 143 | + $this->assertNotNull($controller->widget->partialList ?? null, 'partialList should be registered'); |
| 144 | + $this->assertNotNull($controller->widget->layoutList ?? null, 'layoutList should be registered'); |
| 145 | + $this->assertNotNull($controller->widget->contentList ?? null, 'contentList should be registered'); |
| 146 | + $this->assertNotNull($controller->widget->assetList ?? null, 'assetList should be registered'); |
| 147 | + $this->assertNotNull($controller->widget->componentList ?? null, 'componentList should be registered'); |
| 148 | + } |
| 149 | + |
| 150 | + public function testPagesOnlyUserGetsOnlyPageList(): void |
| 151 | + { |
| 152 | + $user = (new UserFixture)->withPermission('cms.manage_pages', true); |
| 153 | + $this->actingAs($user); |
| 154 | + $controller = new Index; |
| 155 | + |
| 156 | + $this->assertNotNull($controller->widget->pageList ?? null, 'pageList should be registered'); |
| 157 | + $this->assertNull($controller->widget->partialList ?? null, 'partialList should not be registered'); |
| 158 | + $this->assertNull($controller->widget->layoutList ?? null, 'layoutList should not be registered'); |
| 159 | + $this->assertNull($controller->widget->contentList ?? null, 'contentList should not be registered'); |
| 160 | + $this->assertNull($controller->widget->assetList ?? null, 'assetList should not be registered'); |
| 161 | + // Pages user should get componentList since components are usable in pages |
| 162 | + $this->assertNotNull($controller->widget->componentList ?? null, 'componentList should be registered for pages user'); |
| 163 | + } |
| 164 | + |
| 165 | + public function testAssetsOnlyUserGetsOnlyAssetList(): void |
| 166 | + { |
| 167 | + $user = (new UserFixture)->withPermission('cms.manage_assets', true); |
| 168 | + $this->actingAs($user); |
| 169 | + $controller = new Index; |
| 170 | + |
| 171 | + $this->assertNull($controller->widget->pageList ?? null, 'pageList should not be registered'); |
| 172 | + $this->assertNull($controller->widget->partialList ?? null, 'partialList should not be registered'); |
| 173 | + $this->assertNull($controller->widget->layoutList ?? null, 'layoutList should not be registered'); |
| 174 | + $this->assertNull($controller->widget->contentList ?? null, 'contentList should not be registered'); |
| 175 | + $this->assertNotNull($controller->widget->assetList ?? null, 'assetList should be registered'); |
| 176 | + $this->assertNull($controller->widget->componentList ?? null, 'componentList should not be registered for assets-only user'); |
| 177 | + } |
| 178 | + |
| 179 | + public function testPagesAndLayoutsUserGetsComponentList(): void |
| 180 | + { |
| 181 | + $user = (new UserFixture) |
| 182 | + ->withPermission('cms.manage_pages', true) |
| 183 | + ->withPermission('cms.manage_layouts', true); |
| 184 | + $this->actingAs($user); |
| 185 | + $controller = new Index; |
| 186 | + |
| 187 | + $this->assertNotNull($controller->widget->pageList ?? null, 'pageList should be registered'); |
| 188 | + $this->assertNull($controller->widget->partialList ?? null, 'partialList should not be registered'); |
| 189 | + $this->assertNotNull($controller->widget->layoutList ?? null, 'layoutList should be registered'); |
| 190 | + $this->assertNull($controller->widget->contentList ?? null, 'contentList should not be registered'); |
| 191 | + $this->assertNull($controller->widget->assetList ?? null, 'assetList should not be registered'); |
| 192 | + $this->assertNotNull($controller->widget->componentList ?? null, 'componentList should be registered'); |
| 193 | + } |
| 194 | + |
| 195 | + public function testContentOnlyUserDoesNotGetComponentList(): void |
| 196 | + { |
| 197 | + $user = (new UserFixture)->withPermission('cms.manage_content', true); |
| 198 | + $this->actingAs($user); |
| 199 | + $controller = new Index; |
| 200 | + |
| 201 | + $this->assertNotNull($controller->widget->contentList ?? null, 'contentList should be registered'); |
| 202 | + $this->assertNull($controller->widget->componentList ?? null, 'componentList should not be registered for content-only user'); |
| 203 | + } |
| 204 | +} |
0 commit comments