/tests/Codeception/api/com_media/MediaCest.php

https://github.com/joomla/joomla-cms · PHP · 550 lines · 251 code · 59 blank · 240 comment · 2 complexity · b552beb67889fb5fd83fd72aaccd4a26 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Tests
  4. * @subpackage Api.tests
  5. *
  6. * @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
  10. use Codeception\Util\FileSystem;
  11. use Codeception\Util\HttpCode;
  12. /**
  13. * Class MediaCest.
  14. *
  15. * Basic com_media (files) tests.
  16. *
  17. * @since 4.1.0
  18. */
  19. class MediaCest
  20. {
  21. /**
  22. * The name of the test directory, which gets deleted after each test.
  23. *
  24. * @var string
  25. *
  26. * @since 4.1.0
  27. */
  28. private $testDirectory = 'test-dir';
  29. /**
  30. * Runs before every test.
  31. *
  32. * @param ApiTester $I Api tester
  33. *
  34. * @since 4.1.0
  35. *
  36. * @throws Exception
  37. */
  38. public function _before(ApiTester $I)
  39. {
  40. if (file_exists($this->getImagesDirectory($I)))
  41. {
  42. FileSystem::deleteDir($this->getImagesDirectory($I));
  43. }
  44. // Copied from \Step\Acceptance\Administrator\Media:createDirectory()
  45. $oldUmask = @umask(0);
  46. @mkdir($this->getImagesDirectory($I), 0755, true);
  47. if (!empty($user = $I->getConfig('localUser')))
  48. {
  49. @chown($this->getImagesDirectory($I), $user);
  50. }
  51. @umask($oldUmask);
  52. }
  53. /**
  54. * Runs after every test.
  55. *
  56. * @param ApiTester $I Api tester
  57. *
  58. * @since 4.1.0
  59. *
  60. * @throws Exception
  61. */
  62. public function _after(ApiTester $I)
  63. {
  64. // Delete the test directory
  65. FileSystem::deleteDir($this->getImagesDirectory($I));
  66. }
  67. /**
  68. * Test the GET media adapter endpoint of com_media from the API.
  69. *
  70. * @param ApiTester $I Api tester
  71. *
  72. * @return void
  73. *
  74. * @since 4.1.0
  75. */
  76. public function testGetAdapters(ApiTester $I)
  77. {
  78. $I->amBearerAuthenticated($I->getBearerToken());
  79. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  80. $I->sendGET('/media/adapters');
  81. $I->seeResponseCodeIs(HttpCode::OK);
  82. $I->seeResponseContainsJson(['provider_id' => 'local', 'name' => 'images']);
  83. }
  84. /**
  85. * Test the GET media adapter endpoint for a single adapter of com_media from the API.
  86. *
  87. * @param ApiTester $I Api tester
  88. *
  89. * @return void
  90. *
  91. * @since 4.1.0
  92. */
  93. public function testGetAdapter(ApiTester $I)
  94. {
  95. $I->amBearerAuthenticated($I->getBearerToken());
  96. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  97. $I->sendGET('/media/adapters/local-images');
  98. $I->seeResponseCodeIs(HttpCode::OK);
  99. $I->seeResponseContainsJson(['provider_id' => 'local', 'name' => 'images']);
  100. }
  101. /**
  102. * Test the GET media files endpoint of com_media from the API.
  103. *
  104. * @param ApiTester $I Api tester
  105. *
  106. * @return void
  107. *
  108. * @since 4.1.0
  109. */
  110. public function testGetFiles(ApiTester $I)
  111. {
  112. $I->amBearerAuthenticated($I->getBearerToken());
  113. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  114. $I->sendGET('/media/files');
  115. $I->seeResponseCodeIs(HttpCode::OK);
  116. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'dir', 'name' => 'banners']]]);
  117. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'file', 'name' => 'joomla_black.png']]]);
  118. }
  119. /**
  120. * Test the GET media files endpoint of com_media from the API.
  121. *
  122. * @param ApiTester $I Api tester
  123. *
  124. * @return void
  125. *
  126. * @since 4.1.0
  127. */
  128. public function testGetFilesInSubfolder(ApiTester $I)
  129. {
  130. $I->amBearerAuthenticated($I->getBearerToken());
  131. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  132. $I->sendGET('/media/files/sampledata/cassiopeia/');
  133. $I->seeResponseCodeIs(HttpCode::OK);
  134. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'file', 'name' => 'nasa1-1200.jpg']]]);
  135. }
  136. /**
  137. * Test the GET media files endpoint of com_media from the API.
  138. *
  139. * @param ApiTester $I Api tester
  140. *
  141. * @return void
  142. *
  143. * @since 4.1.0
  144. */
  145. public function testGetFilesWithAdapter(ApiTester $I)
  146. {
  147. $I->amBearerAuthenticated($I->getBearerToken());
  148. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  149. $I->sendGET('/media/files/local-images:/sampledata/cassiopeia/');
  150. $I->seeResponseCodeIs(HttpCode::OK);
  151. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'file', 'name' => 'nasa1-1200.jpg']]]);
  152. }
  153. /**
  154. * Test the GET media files endpoint of com_media from the API.
  155. *
  156. * @param ApiTester $I Api tester
  157. *
  158. * @return void
  159. *
  160. * @since 4.1.0
  161. */
  162. public function testSearchFiles(ApiTester $I)
  163. {
  164. $I->amBearerAuthenticated($I->getBearerToken());
  165. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  166. $I->sendGET('/media/files?filter[search]=joomla');
  167. $I->seeResponseCodeIs(HttpCode::OK);
  168. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'file', 'name' => 'joomla_black.png']]]);
  169. $I->dontSeeResponseContainsJson(['data' => ['attributes' => ['type' => 'dir', 'name' => 'powered_by.png']]]);
  170. $I->dontSeeResponseContainsJson(['data' => ['attributes' => ['type' => 'dir', 'name' => 'banners']]]);
  171. }
  172. /**
  173. * Test the GET media files endpoint for a single file of com_media from the API.
  174. *
  175. * @param ApiTester $I Api tester
  176. *
  177. * @return void
  178. *
  179. * @since 4.1.0
  180. */
  181. public function testGetFile(ApiTester $I)
  182. {
  183. $I->amBearerAuthenticated($I->getBearerToken());
  184. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  185. $I->sendGET('/media/files/joomla_black.png');
  186. $I->seeResponseCodeIs(HttpCode::OK);
  187. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'file', 'name' => 'joomla_black.png']]]);
  188. $I->dontSeeResponseContainsJson(['data' => ['attributes' => ['url' => $I->getConfig('url') . '/images/joomla_black.png']]]);
  189. }
  190. /**
  191. * Test the GET media files endpoint for a single file of com_media from the API.
  192. *
  193. * @param ApiTester $I Api tester
  194. *
  195. * @return void
  196. *
  197. * @since 4.1.0
  198. */
  199. public function testGetFileWithUrl(ApiTester $I)
  200. {
  201. $I->amBearerAuthenticated($I->getBearerToken());
  202. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  203. $I->sendGET('/media/files/joomla_black.png?url=1');
  204. $I->seeResponseCodeIs(HttpCode::OK);
  205. $I->seeResponseContainsJson(['data' => ['attributes' => ['url' => $I->getConfig('url') . '/images/joomla_black.png']]]);
  206. }
  207. /**
  208. * Test the GET media files endpoint for a single file of com_media from the API.
  209. *
  210. * @param ApiTester $I Api tester
  211. *
  212. * @return void
  213. *
  214. * @since 4.1.0
  215. */
  216. public function testGetFolder(ApiTester $I)
  217. {
  218. $I->amBearerAuthenticated($I->getBearerToken());
  219. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  220. $I->sendGET('/media/files/sampledata/cassiopeia');
  221. $I->seeResponseCodeIs(HttpCode::OK);
  222. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'dir', 'name' => 'cassiopeia']]]);
  223. }
  224. /**
  225. * Test the POST media files endpoint of com_media from the API without adapter information.
  226. *
  227. * @param ApiTester $I Api tester
  228. *
  229. * @return void
  230. *
  231. * @since 4.1.0
  232. */
  233. public function testCreateFileWithoutAdapter(ApiTester $I)
  234. {
  235. $I->amBearerAuthenticated($I->getBearerToken());
  236. $I->haveHttpHeader('Content-Type', 'application/json');
  237. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  238. $I->sendPost(
  239. '/media/files',
  240. [
  241. 'path' => $this->testDirectory . '/test.jpg',
  242. 'content' => base64_encode(file_get_contents(codecept_data_dir() . '/com_media/test-image-1.jpg'))
  243. ]
  244. );
  245. $I->seeResponseCodeIs(HttpCode::OK);
  246. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'file', 'name' => 'test.jpg']]]);
  247. }
  248. /**
  249. * Test the POST media files endpoint of com_media from the API without adapter information.
  250. *
  251. * @param ApiTester $I Api tester
  252. *
  253. * @return void
  254. *
  255. * @since 4.1.0
  256. */
  257. public function testCreateFolderWithoutAdapter(ApiTester $I)
  258. {
  259. $I->amBearerAuthenticated($I->getBearerToken());
  260. $I->haveHttpHeader('Content-Type', 'application/json');
  261. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  262. $I->sendPost(
  263. '/media/files',
  264. ['path' => $this->testDirectory . '/test-from-create']
  265. );
  266. $I->seeResponseCodeIs(HttpCode::OK);
  267. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'dir', 'name' => 'test-from-create']]]);
  268. }
  269. /**
  270. * Test the POST media files endpoint of com_media from the API with adapter information.
  271. *
  272. * @param ApiTester $I Api tester
  273. *
  274. * @return void
  275. *
  276. * @since 4.1.0
  277. */
  278. public function testCreateFileWithAdapter(ApiTester $I)
  279. {
  280. $I->amBearerAuthenticated($I->getBearerToken());
  281. $I->haveHttpHeader('Content-Type', 'application/json');
  282. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  283. $I->sendPost(
  284. '/media/files',
  285. [
  286. 'path' => 'local-images:/' . $this->testDirectory . '/test.jpg',
  287. 'content' => base64_encode(file_get_contents(codecept_data_dir() . '/com_media/test-image-1.jpg'))
  288. ]
  289. );
  290. $I->seeResponseCodeIs(HttpCode::OK);
  291. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'file', 'name' => 'test.jpg']]]);
  292. }
  293. /**
  294. * Test the POST media files endpoint of com_media from the API with adapter information.
  295. *
  296. * @param ApiTester $I Api tester
  297. *
  298. * @return void
  299. *
  300. * @since 4.1.0
  301. */
  302. public function testCreateFolderWithAdapter(ApiTester $I)
  303. {
  304. $I->amBearerAuthenticated($I->getBearerToken());
  305. $I->haveHttpHeader('Content-Type', 'application/json');
  306. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  307. $I->sendPost(
  308. '/media/files',
  309. ['path' => 'local-images:/' . $this->testDirectory . '/test-from-create']
  310. );
  311. $I->seeResponseCodeIs(HttpCode::OK);
  312. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'dir', 'name' => 'test-from-create']]]);
  313. }
  314. /**
  315. * Test the PATCH media files endpoint of com_media from the API without adapter information.
  316. *
  317. * @param ApiTester $I Api tester
  318. *
  319. * @return void
  320. *
  321. * @since 4.1.0
  322. */
  323. public function testUpdateFileWithoutAdapter(ApiTester $I)
  324. {
  325. file_put_contents($this->getImagesDirectory($I) . '/override.jpg', '1');
  326. $I->amBearerAuthenticated($I->getBearerToken());
  327. $I->haveHttpHeader('Content-Type', 'application/json');
  328. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  329. $I->sendPatch(
  330. '/media/files/' . $this->testDirectory . '/override.jpg',
  331. [
  332. 'path' => $this->testDirectory . '/override.jpg',
  333. 'content' => base64_encode(file_get_contents(codecept_data_dir() . '/com_media/test-image-1.jpg'))
  334. ]
  335. );
  336. $I->seeResponseCodeIs(HttpCode::OK);
  337. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'file', 'name' => 'override.jpg']]]);
  338. $I->dontSeeResponseContainsJson(['data' => ['attributes' => ['content' => '1']]]);
  339. }
  340. /**
  341. * Test the PATCH media files endpoint of com_media from the API without adapter information.
  342. *
  343. * @param ApiTester $I Api tester
  344. *
  345. * @return void
  346. *
  347. * @since 4.1.0
  348. */
  349. public function testUpdateFolderWithoutAdapter(ApiTester $I)
  350. {
  351. mkdir($this->getImagesDirectory($I) . '/override');
  352. $I->amBearerAuthenticated($I->getBearerToken());
  353. $I->haveHttpHeader('Content-Type', 'application/json');
  354. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  355. $I->sendPatch(
  356. '/media/files/' . $this->testDirectory . '/override',
  357. ['path' => $this->testDirectory . '/override-new']
  358. );
  359. $I->seeResponseCodeIs(HttpCode::OK);
  360. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'dir', 'name' => 'override-new']]]);
  361. }
  362. /**
  363. * Test the PATCH media files endpoint of com_media from the API wit adapter information.
  364. *
  365. * @param ApiTester $I Api tester
  366. *
  367. * @return void
  368. *
  369. * @since 4.1.0
  370. */
  371. public function testUpdateFileWithAdapter(ApiTester $I)
  372. {
  373. file_put_contents($this->getImagesDirectory($I) . '/override.jpg', '1');
  374. $I->amBearerAuthenticated($I->getBearerToken());
  375. $I->haveHttpHeader('Content-Type', 'application/json');
  376. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  377. $I->sendPatch(
  378. '/media/files/local-images:/' . $this->testDirectory . '/override.jpg',
  379. [
  380. 'path' => 'local-images:/' . $this->testDirectory . '/override.jpg',
  381. 'content' => base64_encode(file_get_contents(codecept_data_dir() . '/com_media/test-image-1.jpg'))
  382. ]
  383. );
  384. $I->seeResponseCodeIs(HttpCode::OK);
  385. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'file', 'name' => 'override.jpg']]]);
  386. $I->dontSeeResponseContainsJson(['data' => ['attributes' => ['content' => '1']]]);
  387. }
  388. /**
  389. * Test the PATCH media files endpoint of com_media from the API with adapter information.
  390. *
  391. * @param ApiTester $I Api tester
  392. *
  393. * @return void
  394. *
  395. * @since 4.1.0
  396. */
  397. public function testUpdateFolderWithAdapter(ApiTester $I)
  398. {
  399. mkdir($this->getImagesDirectory($I) . '/override');
  400. $I->amBearerAuthenticated($I->getBearerToken());
  401. $I->haveHttpHeader('Content-Type', 'application/json');
  402. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  403. $I->sendPatch(
  404. '/media/files/local-images:/' . $this->testDirectory . '/override',
  405. ['path' => 'local-images:/' . $this->testDirectory . '/override-new']
  406. );
  407. $I->seeResponseCodeIs(HttpCode::OK);
  408. $I->seeResponseContainsJson(['data' => ['attributes' => ['type' => 'dir', 'name' => 'override-new']]]);
  409. }
  410. /**
  411. * Test the DELETE media files endpoint of com_media from the API without adapter information.
  412. *
  413. * @param ApiTester $I Api tester
  414. *
  415. * @return void
  416. *
  417. * @since 4.1.0
  418. */
  419. public function testDeleteFileWithoutAdapter(ApiTester $I)
  420. {
  421. touch($this->getImagesDirectory($I) . '/todelete.jpg');
  422. $I->amBearerAuthenticated($I->getBearerToken());
  423. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  424. $I->sendDelete('/media/files/' . $this->testDirectory . '/todelete.jpg');
  425. $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
  426. }
  427. /**
  428. * Test the DELETE media files endpoint of com_media from the API without adapter information.
  429. *
  430. * @param ApiTester $I Api tester
  431. *
  432. * @return void
  433. *
  434. * @since 4.1.0
  435. */
  436. public function testDeleteFolderWithoutAdapter(ApiTester $I)
  437. {
  438. mkdir($this->getImagesDirectory($I) . '/todelete');
  439. $I->amBearerAuthenticated($I->getBearerToken());
  440. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  441. $I->sendDelete('/media/files/' . $this->testDirectory . '/todelete');
  442. $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
  443. }
  444. /**
  445. * Test the DELETE media files endpoint of com_media from the API with adapter information.
  446. *
  447. * @param ApiTester $I Api tester
  448. *
  449. * @return void
  450. *
  451. * @since 4.1.0
  452. */
  453. public function testDeleteFileWithAdapter(ApiTester $I)
  454. {
  455. touch($this->getImagesDirectory($I) . '/todelete.jpg');
  456. $I->amBearerAuthenticated($I->getBearerToken());
  457. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  458. $I->sendDelete('/media/files/local-images:/' . $this->testDirectory . '/todelete.jpg');
  459. $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
  460. }
  461. /**
  462. * Test the DELETE media files endpoint of com_media from the API with adapter information.
  463. *
  464. * @param ApiTester $I Api tester
  465. *
  466. * @return void
  467. *
  468. * @since 4.1.0
  469. */
  470. public function testDeleteFolderWithAdapter(ApiTester $I)
  471. {
  472. mkdir($this->getImagesDirectory($I) . '/todelete');
  473. $I->amBearerAuthenticated($I->getBearerToken());
  474. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  475. $I->sendDelete('/media/files/local-images:/' . $this->testDirectory . '/todelete');
  476. $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
  477. }
  478. /**
  479. * Returns the absolute tmp image folder path to work on.
  480. *
  481. * @param ApiTester $I Api tester
  482. *
  483. * @return string The absolute folder path
  484. *
  485. * @since 4.1.0
  486. */
  487. private function getImagesDirectory(ApiTester $I): string
  488. {
  489. return $I->getConfig('cmsPath') . '/images/' . $this->testDirectory;
  490. }
  491. }