/tests/src/Unit/CdnSettingsTest.php

https://gitlab.com/Drulenium-bot/cdn · PHP · 336 lines · 269 code · 14 blank · 53 comment · 0 complexity · 43e15f568b83018cf00b99b60de68653 MD5 · raw file

  1. <?php
  2. namespace Drupal\Tests\cdn\Unit;
  3. use Drupal\cdn\CdnSettings;
  4. use Drupal\Tests\UnitTestCase;
  5. /**
  6. * @coversDefaultClass \Drupal\cdn\CdnSettings
  7. * @group cdn
  8. */
  9. class CdnSettingsTest extends UnitTestCase {
  10. /**
  11. * @covers ::getLookupTable
  12. * @covers ::getDomains
  13. * @dataProvider settingsProvider
  14. */
  15. public function test(array $raw_config, array $expected_lookup_table, array $expected_domains) {
  16. $cdn_settings = $this->createCdnSettings($raw_config);
  17. $this->assertTrue($cdn_settings->isEnabled());
  18. $this->assertSame($expected_lookup_table, $cdn_settings->getLookupTable());
  19. $this->assertSame(array_values($expected_domains), array_values($cdn_settings->getDomains()));
  20. }
  21. public function settingsProvider() {
  22. return [
  23. 'simple, no conditions' => [
  24. [
  25. 'status' => 2,
  26. 'mapping' => [
  27. 'type' => 'simple',
  28. 'domain' => 'cdn.example.com',
  29. 'conditions' => [],
  30. ],
  31. ],
  32. ['*' => 'cdn.example.com'],
  33. ['cdn.example.com'],
  34. ],
  35. 'simple, oone empty condition' => [
  36. [
  37. 'status' => 2,
  38. 'mapping' => [
  39. 'type' => 'simple',
  40. 'domain' => 'cdn.example.com',
  41. 'conditions' => [
  42. 'extensions' => [],
  43. ],
  44. ],
  45. ],
  46. ['*' => 'cdn.example.com'],
  47. ['cdn.example.com'],
  48. ],
  49. 'simple, on, one condition' => [
  50. [
  51. 'status' => 2,
  52. 'mapping' => [
  53. 'type' => 'simple',
  54. 'domain' => 'cdn.example.com',
  55. 'conditions' => [
  56. 'extensions' => ['jpg', 'jpeg', 'png'],
  57. ],
  58. ],
  59. ],
  60. [
  61. 'jpg' => 'cdn.example.com',
  62. 'jpeg' => 'cdn.example.com',
  63. 'png' => 'cdn.example.com',
  64. ],
  65. ['cdn.example.com'],
  66. ],
  67. 'auto-balanced, on, no fallback' => [
  68. [
  69. 'status' => 2,
  70. 'mapping' => [
  71. 'type' => 'auto-balanced',
  72. 'domains' => [
  73. 'img1.example.com',
  74. 'img2.example.com',
  75. ],
  76. 'conditions' => [
  77. 'extensions' => ['jpg', 'png'],
  78. ]
  79. ],
  80. ],
  81. [
  82. 'jpg' => ['img1.example.com', 'img2.example.com'],
  83. 'png' => ['img1.example.com', 'img2.example.com'],
  84. ],
  85. ['img1.example.com', 'img2.example.com'],
  86. ],
  87. 'complex containing two simple mappings, with fallback' => [
  88. [
  89. 'status' => 2,
  90. 'mapping' => [
  91. 'type' => 'complex',
  92. 'fallback_domain' => 'cdn.example.com',
  93. 'domains' => [
  94. 0 => [
  95. 'type' => 'simple',
  96. 'domain' => 'static.example.com',
  97. 'conditions' => [
  98. 'extensions' => ['css', 'jpg', 'jpeg', 'png'],
  99. ],
  100. ],
  101. 1 => [
  102. 'type' => 'simple',
  103. 'domain' => 'downloads.example.com',
  104. 'conditions' => [
  105. 'extensions' => ['zip'],
  106. ],
  107. ]
  108. ],
  109. ],
  110. ],
  111. [
  112. '*' => 'cdn.example.com',
  113. 'css' => 'static.example.com',
  114. 'jpg' => 'static.example.com',
  115. 'jpeg' => 'static.example.com',
  116. 'png' => 'static.example.com',
  117. 'zip' => 'downloads.example.com',
  118. ],
  119. ['cdn.example.com', 'static.example.com', 'downloads.example.com'],
  120. ],
  121. 'complex containing two simple mappings, without fallback' => [
  122. [
  123. 'status' => 2,
  124. 'mapping' => [
  125. 'type' => 'complex',
  126. 'fallback_domain' => NULL,
  127. 'domains' => [
  128. 0 => [
  129. 'type' => 'simple',
  130. 'domain' => 'static.example.com',
  131. 'conditions' => [
  132. 'extensions' => ['css', 'jpg', 'jpeg', 'png'],
  133. ],
  134. ],
  135. 1 => [
  136. 'type' => 'simple',
  137. 'domain' => 'downloads.example.com',
  138. 'conditions' => [
  139. 'extensions' => ['zip'],
  140. ],
  141. ]
  142. ],
  143. ],
  144. ],
  145. [
  146. 'css' => 'static.example.com',
  147. 'jpg' => 'static.example.com',
  148. 'jpeg' => 'static.example.com',
  149. 'png' => 'static.example.com',
  150. 'zip' => 'downloads.example.com',
  151. ],
  152. ['static.example.com', 'downloads.example.com'],
  153. ],
  154. 'complex containing one simple and one auto-balanced mapping, without fallback' => [
  155. [
  156. 'status' => 2,
  157. 'mapping' => [
  158. 'type' => 'complex',
  159. 'fallback_domain' => NULL,
  160. 'domains' => [
  161. 0 => [
  162. 'type' => 'simple',
  163. 'domain' => 'static.example.com',
  164. 'conditions' => [
  165. 'extensions' => ['css', 'js'],
  166. ],
  167. ],
  168. 1 => [
  169. 'type' => 'auto-balanced',
  170. 'domains' => [
  171. 'img1.example.com',
  172. 'img2.example.com',
  173. ],
  174. 'conditions' => [
  175. 'extensions' => ['jpg', 'jpeg', 'png'],
  176. ],
  177. ]
  178. ],
  179. ],
  180. ],
  181. [
  182. 'css' => 'static.example.com',
  183. 'js' => 'static.example.com',
  184. 'jpg' => ['img1.example.com', 'img2.example.com'],
  185. 'jpeg' => ['img1.example.com', 'img2.example.com'],
  186. 'png' => ['img1.example.com', 'img2.example.com'],
  187. ],
  188. ['static.example.com', 'img1.example.com', 'img2.example.com'],
  189. ],
  190. ];
  191. }
  192. /**
  193. * @covers ::getLookupTable
  194. * @expectedException \AssertionError
  195. * @expectedExceptionMessage The provided domain http://cdn.example.com is not a valid domain. Provide domains or hostnames of the form 'cdn.com', 'cdn.example.com'. IP addresses and ports are also allowed.
  196. */
  197. public function testAbsoluteUrlAsSimpleDomain() {
  198. $this->createCdnSettings([
  199. 'status' => 2,
  200. 'mapping' => [
  201. 'type' => 'simple',
  202. 'domain' => 'http://cdn.example.com'
  203. ],
  204. ])->getLookupTable();
  205. }
  206. /**
  207. * @covers ::getLookupTable
  208. * @expectedException \AssertionError
  209. * @expectedExceptionMessage The provided domain //cdn.example.com is not a valid domain. Provide domains or hostnames of the form 'cdn.com', 'cdn.example.com'. IP addresses and ports are also allowed.
  210. */
  211. public function testProtocolRelativeUrlAsSimpleDomain() {
  212. $this->createCdnSettings([
  213. 'status' => 2,
  214. 'mapping' => [
  215. 'type' => 'simple',
  216. 'domain' => '//cdn.example.com'
  217. ],
  218. ])->getLookupTable();
  219. }
  220. /**
  221. * @covers ::getLookupTable
  222. * @expectedException \AssertionError
  223. * @expectedExceptionMessage The provided fallback domain http://cdn.example.com is not a valid domain. Provide domains or hostnames of the form 'cdn.com', 'cdn.example.com'. IP addresses and ports are also allowed.
  224. */
  225. public function testAbsoluteUrlAsComplexFallbackDomain() {
  226. $this->createCdnSettings([
  227. 'status' => 2,
  228. 'mapping' => [
  229. 'type' => 'complex',
  230. 'fallback_domain' => 'http://cdn.example.com'
  231. ],
  232. ])->getLookupTable();
  233. }
  234. /**
  235. * @covers ::getLookupTable
  236. * @expectedException \AssertionError
  237. * @expectedExceptionMessage The provided fallback domain //cdn.example.com is not a valid domain. Provide domains or hostnames of the form 'cdn.com', 'cdn.example.com'. IP addresses and ports are also allowed.
  238. */
  239. public function testProtocolRelativeUrlAsComplexFallbackDomain() {
  240. $this->createCdnSettings([
  241. 'status' => 2,
  242. 'mapping' => [
  243. 'type' => 'complex',
  244. 'fallback_domain' => '//cdn.example.com'
  245. ],
  246. ])->getLookupTable();
  247. }
  248. /**
  249. * @covers ::getLookupTable
  250. * @expectedException \AssertionError
  251. * @expectedExceptionMessage The provided domain http://foo.example.com is not a valid domain. Provide domains or hostnames of the form 'cdn.com', 'cdn.example.com'. IP addresses and ports are also allowed.
  252. */
  253. public function testAbsoluteUrlAsAutobalancedDomain() {
  254. $this->createCdnSettings([
  255. 'status' => 2,
  256. 'mapping' => [
  257. 'type' => 'auto-balanced',
  258. 'domains' => [
  259. 'http://foo.example.com',
  260. 'http://bar.example.com',
  261. ],
  262. 'conditions' => [
  263. 'extensions' => [
  264. 'png',
  265. ],
  266. ],
  267. ],
  268. ])->getLookupTable();
  269. }
  270. /**
  271. * @covers ::getLookupTable
  272. * @expectedException \AssertionError
  273. * @expectedExceptionMessage The provided domain //foo.example.com is not a valid domain. Provide domains or hostnames of the form 'cdn.com', 'cdn.example.com'. IP addresses and ports are also allowed.
  274. */
  275. public function testProtocolRelativeUrlAsAutobalancedDomain() {
  276. $this->createCdnSettings([
  277. 'status' => 2,
  278. 'mapping' => [
  279. 'type' => 'auto-balanced',
  280. 'domains' => [
  281. '//foo.example.com',
  282. '//bar.example.com',
  283. ],
  284. 'conditions' => [
  285. 'extensions' => [
  286. 'png',
  287. ],
  288. ],
  289. ],
  290. ])->getLookupTable();
  291. }
  292. /**
  293. * @covers ::getLookupTable
  294. * @expectedException \Drupal\Core\Config\ConfigValueException
  295. * @expectedExceptionMessage It does not make sense to apply auto-balancing to all files, regardless of extension.
  296. */
  297. public function testAutobalancedWithoutConditions() {
  298. $this->createCdnSettings([
  299. 'status' => 2,
  300. 'mapping' => [
  301. 'type' => 'auto-balanced',
  302. 'fallback_domain' => NULL,
  303. 'domains' => [
  304. 'foo.example.com',
  305. 'bar.example.com',
  306. ],
  307. ],
  308. ])->getLookupTable();
  309. }
  310. /**
  311. * Creates a CdnSettings object from raw config.
  312. *
  313. * @param array $raw_config
  314. * The raw config for the cdn.settings.yml config.
  315. *
  316. * @return \Drupal\cdn\CdnSettings
  317. * The CdnSettings object to test.
  318. */
  319. protected function createCdnSettings(array $raw_config) {
  320. return new CdnSettings($this->getConfigFactoryStub(['cdn.settings' => $raw_config]));
  321. }
  322. }