|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of Ymir WordPress plugin. |
| 7 | + * |
| 8 | + * (c) Carl Alexander <support@ymirapp.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Ymir\Plugin\Tests\Unit\Configuration; |
| 15 | + |
| 16 | +use Ymir\Plugin\Configuration\AssetsConfiguration; |
| 17 | +use Ymir\Plugin\DependencyInjection\Container; |
| 18 | +use Ymir\Plugin\Tests\Unit\TestCase; |
| 19 | +use Ymir\Plugin\ValueObject\MappedDomainNames; |
| 20 | + |
| 21 | +class AssetsConfigurationTest extends TestCase |
| 22 | +{ |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + putenv('YMIR_ASSETS_PATH=assets/uuid'); |
| 28 | + putenv('YMIR_ASSETS_URL=https://foo.com/assets/uuid'); |
| 29 | + putenv('YMIR_CUSTOM_ASSETS_URL'); |
| 30 | + } |
| 31 | + |
| 32 | + protected function tearDown(): void |
| 33 | + { |
| 34 | + putenv('YMIR_ASSETS_PATH'); |
| 35 | + putenv('YMIR_ASSETS_URL'); |
| 36 | + putenv('YMIR_CUSTOM_ASSETS_URL'); |
| 37 | + |
| 38 | + parent::tearDown(); |
| 39 | + } |
| 40 | + |
| 41 | + public function provideAssetsUrls(): array |
| 42 | + { |
| 43 | + return [ |
| 44 | + 'single site with local assets url' => [ |
| 45 | + 'https://foo.com/assets/uuid', |
| 46 | + false, |
| 47 | + false, |
| 48 | + 'foo.com', |
| 49 | + new MappedDomainNames([], 'foo.com'), |
| 50 | + 'https://foo.com/assets/uuid', |
| 51 | + ], |
| 52 | + 'subdomain multisite with mapped domain and local assets url' => [ |
| 53 | + 'https://foo.com/assets/uuid', |
| 54 | + true, |
| 55 | + true, |
| 56 | + 'foo.com', |
| 57 | + new MappedDomainNames([], 'foo.com'), |
| 58 | + 'https://foo.com/test/assets/uuid', |
| 59 | + ], |
| 60 | + 'subdomain multisite with unmapped domain and local assets url' => [ |
| 61 | + 'https://foo.com/assets/uuid', |
| 62 | + true, |
| 63 | + true, |
| 64 | + 'foo.com', |
| 65 | + new MappedDomainNames([], 'bar.com'), |
| 66 | + 'https://foo.com/assets/uuid', |
| 67 | + ], |
| 68 | + 'subdomain multisite with mapped domain and cloudfront assets url' => [ |
| 69 | + 'https://cloudfront.net/assets/uuid', |
| 70 | + true, |
| 71 | + true, |
| 72 | + 'foo.com', |
| 73 | + new MappedDomainNames([], 'foo.com'), |
| 74 | + 'https://cloudfront.net/assets/uuid', |
| 75 | + ], |
| 76 | + 'subdomain multisite with mapped domain and s3 assets url' => [ |
| 77 | + 'https://s3.amazonaws.com/bucket/assets/uuid', |
| 78 | + true, |
| 79 | + true, |
| 80 | + 'foo.com', |
| 81 | + new MappedDomainNames([], 'foo.com'), |
| 82 | + 'https://s3.amazonaws.com/bucket/assets/uuid', |
| 83 | + ], |
| 84 | + 'subdirectory multisite with same-domain local assets url' => [ |
| 85 | + 'https://foo.com/assets/uuid', |
| 86 | + true, |
| 87 | + false, |
| 88 | + 'foo.com', |
| 89 | + new MappedDomainNames([], 'foo.com'), |
| 90 | + 'https://foo.com/test/assets/uuid', |
| 91 | + ], |
| 92 | + 'subdirectory multisite with different-domain local assets url' => [ |
| 93 | + 'https://assets.com/assets/uuid', |
| 94 | + true, |
| 95 | + false, |
| 96 | + 'foo.com', |
| 97 | + new MappedDomainNames([], 'foo.com'), |
| 98 | + 'https://assets.com/assets/uuid', |
| 99 | + ], |
| 100 | + 'subdirectory multisite with cloudfront assets url' => [ |
| 101 | + 'https://cloudfront.net/assets/uuid', |
| 102 | + true, |
| 103 | + false, |
| 104 | + 'foo.com', |
| 105 | + new MappedDomainNames([], 'foo.com'), |
| 106 | + 'https://cloudfront.net/assets/uuid', |
| 107 | + ], |
| 108 | + 'subdirectory multisite with s3 assets url' => [ |
| 109 | + 'https://s3.amazonaws.com/bucket/assets/uuid', |
| 110 | + true, |
| 111 | + false, |
| 112 | + 'foo.com', |
| 113 | + new MappedDomainNames([], 'foo.com'), |
| 114 | + 'https://s3.amazonaws.com/bucket/assets/uuid', |
| 115 | + ], |
| 116 | + ]; |
| 117 | + } |
| 118 | + |
| 119 | + public function testAssetsPathDefaultsToEmptyString() |
| 120 | + { |
| 121 | + putenv('YMIR_ASSETS_PATH'); |
| 122 | + |
| 123 | + $container = new Container(); |
| 124 | + |
| 125 | + (new AssetsConfiguration())->modify($container); |
| 126 | + |
| 127 | + $this->assertSame('', $container['assets_path']); |
| 128 | + } |
| 129 | + |
| 130 | + public function testAssetsPathUsesEnvironmentVariable() |
| 131 | + { |
| 132 | + $container = new Container(); |
| 133 | + |
| 134 | + (new AssetsConfiguration())->modify($container); |
| 135 | + |
| 136 | + $this->assertSame('assets/uuid', $container['assets_path']); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @dataProvider provideAssetsUrls |
| 141 | + */ |
| 142 | + public function testAssetsUrl(string $assetsUrl, bool $isMultisite, bool $isMultisiteSubdomain, string $siteDomain, MappedDomainNames $mappedDomainNames, string $expectedAssetsUrl) |
| 143 | + { |
| 144 | + putenv('YMIR_ASSETS_URL='.$assetsUrl); |
| 145 | + |
| 146 | + $container = $this->createContainer($isMultisite, $isMultisiteSubdomain, $siteDomain, $mappedDomainNames); |
| 147 | + |
| 148 | + (new AssetsConfiguration())->modify($container); |
| 149 | + |
| 150 | + $this->assertSame($expectedAssetsUrl, $container['assets_url']); |
| 151 | + } |
| 152 | + |
| 153 | + public function testAssetsUrlDefaultsToEmptyString() |
| 154 | + { |
| 155 | + putenv('YMIR_ASSETS_URL'); |
| 156 | + |
| 157 | + $container = $this->createContainer(false, false, 'foo.com', new MappedDomainNames([], 'foo.com')); |
| 158 | + |
| 159 | + (new AssetsConfiguration())->modify($container); |
| 160 | + |
| 161 | + $this->assertSame('', $container['assets_url']); |
| 162 | + } |
| 163 | + |
| 164 | + public function testAssetsUrlUsesCustomAssetsUrl() |
| 165 | + { |
| 166 | + putenv('YMIR_CUSTOM_ASSETS_URL=https://assets.com/custom/'); |
| 167 | + |
| 168 | + $container = new Container(); |
| 169 | + |
| 170 | + (new AssetsConfiguration())->modify($container); |
| 171 | + |
| 172 | + $this->assertSame('https://assets.com/custom/assets/uuid', $container['assets_url']); |
| 173 | + } |
| 174 | + |
| 175 | + private function createContainer(bool $isMultisite, bool $isMultisiteSubdomain, string $siteDomain, MappedDomainNames $mappedDomainNames): Container |
| 176 | + { |
| 177 | + return new Container([ |
| 178 | + 'home_url' => 'https://foo.com/test', |
| 179 | + 'is_multisite' => $isMultisite, |
| 180 | + 'is_multisite_subdomain' => $isMultisiteSubdomain, |
| 181 | + 'site_domain' => $siteDomain, |
| 182 | + 'ymir_mapped_domain_names' => $mappedDomainNames, |
| 183 | + ]); |
| 184 | + } |
| 185 | +} |
0 commit comments