/tests/Codeception/api/com_content/ContentCest.php

https://github.com/Hackwar/joomla-cms · PHP · 148 lines · 75 code · 18 blank · 55 comment · 0 complexity · 457b80e0a5c906486e28b407e70341d4 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Tests
  4. * @subpackage Api.tests
  5. *
  6. * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. use Codeception\Util\HttpCode;
  10. /**
  11. * Class ContentCest.
  12. *
  13. * Basic com_content (article) tests.
  14. *
  15. * @since 4.0.0
  16. */
  17. class ContentCest
  18. {
  19. /**
  20. * Api test before running.
  21. *
  22. * @param mixed ApiTester $I Api tester
  23. *
  24. * @return void
  25. *
  26. * @since 4.0.0
  27. */
  28. public function _before(ApiTester $I)
  29. {
  30. // TODO: Improve this to retrieve a specific ID to replace with a known ID
  31. $desiredUserId = 3;
  32. $I->updateInDatabase('users', ['id' => 3], []);
  33. $I->updateInDatabase('user_usergroup_map', ['user_id' => 3], []);
  34. $enabledData = ['user_id' => $desiredUserId, 'profile_key' => 'joomlatoken.enabled', 'profile_value' => 1];
  35. $tokenData = ['user_id' => $desiredUserId, 'profile_key' => 'joomlatoken.token', 'profile_value' => 'dOi2m1NRrnBHlhaWK/WWxh3B5tqq1INbdf4DhUmYTI4='];
  36. $I->haveInDatabase('user_profiles', $enabledData);
  37. $I->haveInDatabase('user_profiles', $tokenData);
  38. }
  39. /**
  40. * Api test after running.
  41. *
  42. * @param mixed ApiTester $I Api tester
  43. *
  44. * @return void
  45. *
  46. * @since 4.0.0
  47. */
  48. public function _after(ApiTester $I)
  49. {
  50. }
  51. /**
  52. * Test the article crud endpoints of com_content from the API.
  53. *
  54. * @param mixed ApiTester $I Api tester
  55. *
  56. * @return void
  57. *
  58. * @since 4.0.0
  59. *
  60. * @TODO: Make these separate tests but requires sample data being installed so there are existing articles
  61. */
  62. public function testCrudOnArticle(ApiTester $I)
  63. {
  64. $I->amBearerAuthenticated('c2hhMjU2OjM6ZTJmMjJlYTNlNTU0NmM1MDJhYTIzYzMwN2MxYzAwZTQ5NzJhMWRmOTUyNjY5MTk2YjE5ODJmZWMwZTcxNzgwMQ==');
  65. $I->haveHttpHeader('Content-Type', 'application/json');
  66. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  67. $testarticle = [
  68. 'title' => 'Just for you',
  69. 'catid' => 2,
  70. 'articletext' => 'A dummy article to save to the database',
  71. 'language' => '*',
  72. 'alias' => 'tobias'
  73. ];
  74. $I->sendPOST('/content/articles', $testarticle);
  75. $I->seeResponseCodeIs(HttpCode::OK);
  76. $I->amBearerAuthenticated('c2hhMjU2OjM6ZTJmMjJlYTNlNTU0NmM1MDJhYTIzYzMwN2MxYzAwZTQ5NzJhMWRmOTUyNjY5MTk2YjE5ODJmZWMwZTcxNzgwMQ==');
  77. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  78. $I->sendGET('/content/articles/1');
  79. $I->seeResponseCodeIs(HttpCode::OK);
  80. $I->amBearerAuthenticated('c2hhMjU2OjM6ZTJmMjJlYTNlNTU0NmM1MDJhYTIzYzMwN2MxYzAwZTQ5NzJhMWRmOTUyNjY5MTk2YjE5ODJmZWMwZTcxNzgwMQ==');
  81. $I->haveHttpHeader('Content-Type', 'application/json');
  82. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  83. $I->sendPATCH('/content/articles/1', ['title' => 'Another Title', 'state' => -2, 'catid' => 2]);
  84. $I->seeResponseCodeIs(HttpCode::OK);
  85. $I->amBearerAuthenticated('c2hhMjU2OjM6ZTJmMjJlYTNlNTU0NmM1MDJhYTIzYzMwN2MxYzAwZTQ5NzJhMWRmOTUyNjY5MTk2YjE5ODJmZWMwZTcxNzgwMQ==');
  86. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  87. $I->sendDELETE('/content/articles/1');
  88. $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
  89. }
  90. /**
  91. * Test the category crud endpoints of com_content from the API.
  92. *
  93. * @param mixed ApiTester $I Api tester
  94. *
  95. * @return void
  96. *
  97. * @since 4.0.0
  98. *
  99. * @TODO: Make these separate tests but requires sample data being installed so there are existing categories
  100. */
  101. public function testCrudOnCategory(ApiTester $I)
  102. {
  103. $I->amBearerAuthenticated('c2hhMjU2OjM6ZTJmMjJlYTNlNTU0NmM1MDJhYTIzYzMwN2MxYzAwZTQ5NzJhMWRmOTUyNjY5MTk2YjE5ODJmZWMwZTcxNzgwMQ==');
  104. $I->haveHttpHeader('Content-Type', 'application/json');
  105. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  106. $testarticle = [
  107. 'title' => 'A test category',
  108. 'parent_id' => 2,
  109. 'params' => [
  110. 'workflow_id' => 'inherit'
  111. ]
  112. ];
  113. $I->sendPOST('/content/categories', $testarticle);
  114. $I->seeResponseCodeIs(HttpCode::OK);
  115. $categoryId = $I->grabDataFromResponseByJsonPath('$.data.id')[0];
  116. $I->amBearerAuthenticated('c2hhMjU2OjM6ZTJmMjJlYTNlNTU0NmM1MDJhYTIzYzMwN2MxYzAwZTQ5NzJhMWRmOTUyNjY5MTk2YjE5ODJmZWMwZTcxNzgwMQ==');
  117. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  118. $I->sendGET('/content/categories/' . $categoryId);
  119. $I->seeResponseCodeIs(HttpCode::OK);
  120. $I->amBearerAuthenticated('c2hhMjU2OjM6ZTJmMjJlYTNlNTU0NmM1MDJhYTIzYzMwN2MxYzAwZTQ5NzJhMWRmOTUyNjY5MTk2YjE5ODJmZWMwZTcxNzgwMQ==');
  121. $I->haveHttpHeader('Content-Type', 'application/json');
  122. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  123. $I->sendPATCH('/content/categories/' . $categoryId, ['title' => 'Another Title', 'params' => ['workflow_id' => 'inherit'], 'published' => -2]);
  124. $I->seeResponseCodeIs(HttpCode::OK);
  125. $I->amBearerAuthenticated('c2hhMjU2OjM6ZTJmMjJlYTNlNTU0NmM1MDJhYTIzYzMwN2MxYzAwZTQ5NzJhMWRmOTUyNjY5MTk2YjE5ODJmZWMwZTcxNzgwMQ==');
  126. $I->haveHttpHeader('Accept', 'application/vnd.api+json');
  127. $I->sendDELETE('/content/categories/' . $categoryId);
  128. $I->seeResponseCodeIs(HttpCode::NO_CONTENT);
  129. }
  130. }