PageRenderTime 70ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/pockets/tests/APIv2/PocketsTest.php

https://github.com/vanilla/vanilla
PHP | 258 lines | 157 code | 29 blank | 72 comment | 9 complexity | aa657ef2143dc4fb0c9d9b5ba969d4c0 MD5 | raw file
Possible License(s): GPL-2.0, MIT, AGPL-1.0
  1. <?php
  2. /**
  3. * @author Dani Stark<dani.stark@vanillaforums.com>
  4. * @copyright 2009-2021 Vanilla Forums Inc.
  5. * @license Proprietary
  6. */
  7. use VanillaTests\APIv2\AbstractResourceTest;
  8. use VanillaTests\Forum\Utils\CommunityApiTestTrait;
  9. /**
  10. * Class PocketsTest
  11. */
  12. class PocketsTest extends AbstractResourceTest {
  13. use CommunityApiTestTrait;
  14. /** @inheritdoc */
  15. protected static $addons = ['vanilla', 'pockets'];
  16. /** @var array Test data array */
  17. protected static $data;
  18. /** @var string The name of the primary key of the resource. */
  19. protected $pk = "pocketID";
  20. /** {@inheritdoc} */
  21. protected $testPagingOnIndex = false;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function __construct($name = null, array $data = [], $dataName = '') {
  26. $this->baseUrl = '/pockets';
  27. parent::__construct($name, $data, $dataName);
  28. $this->editFields = ['name', 'body', 'repeatType', 'page', 'sort', 'location', 'format', 'mobileType', 'isDashboard', 'isEmbeddable', 'isAd', 'enabled'];
  29. $this->patchFields = ['name', 'body', 'repeatType', 'page', 'sort', 'location', 'format', 'mobileType', 'isDashboard', 'isEmbeddable', 'isAd', 'enabled'];
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function setupBeforeClass(): void {
  35. parent::setupBeforeClass();
  36. /**
  37. * @var \Gdn_Session $session
  38. */
  39. $session = self::container()->get(\Gdn_Session::class);
  40. $session->start(self::$siteInfo['adminUserID'], false, false);
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function modifyRow(array $row) {
  46. $newRow = [];
  47. $dt = new \DateTimeImmutable();
  48. foreach ($this->patchFields as $key) {
  49. $value = $row[$key];
  50. if (in_array($key, ['name', 'body', 'description'])) {
  51. $value .= ' '.$dt->format(\DateTime::RSS);
  52. } elseif (stripos($key, 'id') === strlen($key) - 2) {
  53. $value++;
  54. }
  55. $newRow[$key] = $value;
  56. }
  57. return $newRow;
  58. }
  59. /**
  60. * Grab values for inserting a new pocket.
  61. *
  62. * @param string $name Name of the pocket
  63. * @return array
  64. */
  65. public function record(string $name = 'Test Pocket'): array {
  66. $record = $this->getPocketTemplate();
  67. $record['name'] = $name;
  68. foreach ($record as $key => $value) {
  69. if ($value === '' || $value === null) {
  70. unset($record[$key]);
  71. }
  72. }
  73. return $record;
  74. }
  75. /**
  76. * Test getting pocket by id.
  77. *
  78. * @depends testPostPocket
  79. */
  80. public function testGet(): void {
  81. $result = $this->api()->get("/pockets/".self::$data['pocketID']);
  82. $this->assertEquals(200, $result->getStatusCode());
  83. $resultBody = $result->getBody();
  84. $expandedBodyExists = isset($resultBody['body']);
  85. $this->assertEquals(false, $expandedBodyExists);
  86. $resultExpand = $this->api()->get("/pockets/".self::$data['pocketID']."?expand=body");
  87. $this->assertEquals(200, $resultExpand->getStatusCode());
  88. $resultBody = $resultExpand->getBody();
  89. $expandedBodyExists = isset($resultBody['body']);
  90. $this->assertEquals(true, $expandedBodyExists);
  91. }
  92. /**
  93. * Provide pocket test data.
  94. *
  95. * @return array
  96. */
  97. public function providePocketFields(): array {
  98. $r = [
  99. //name/body/widgetID/repeatType/repeatEvery/repeastIndexes/mobileType/roleIDs/isEmbeddable/isAd/page/enabled/
  100. //isDashboard/location/categoryID/includeChildCategories/format/roleIDs
  101. 'pocket-widget' => [["body" => "<html>\n <body>\n test pocket\n </body>\n</html>"], null],
  102. 'repeatType-every-success' => [['repeatType' => "every", "repeatEvery" => "1" ], null],
  103. 'repeatType-every-failure' => [['repeatType' => "every"], 400],
  104. 'repeatType-index-success' => [['repeatType' => "index", "repeatIndexes" => [1,2]], null],
  105. 'repeatType-index-failure' => [['repeatType' => "index"], 400],
  106. 'repeatType-after' => [['repeatType' => "after"], null],
  107. 'repeatType-before' => [['repeatType' => "before"], null],
  108. 'repeatType-once' => [['repeatType' => "once"], null],
  109. 'repeatType-fail' => [['repeatType' => "wrongType"], 422],
  110. 'mobileType-only' => [['mobileType'=> 'only'], null],
  111. 'mobileType-never' => [['mobileType'=> 'never'], null],
  112. 'mobileType-default' => [['mobileType'=> 'default'], null],
  113. 'mobileType-fail' => [['mobileType'=> 'wrongType'], 422],
  114. 'isEmbeddable-true' => [['isEmbeddable'=> true], null],
  115. 'isAd-true' => [['isAd'=> true], null],
  116. 'page-activity' => [['page'=> 'activity'], null],
  117. 'page-categories' => [['page'=> 'categories'], null],
  118. 'page-discussions' => [['page'=> 'discussions'], null],
  119. 'page-home' => [['page'=> 'home'], null],
  120. 'page-inbox' => [['page'=> 'inbox'], null],
  121. 'page-profile' => [['page'=> 'profile'], null],
  122. 'page-invalid' => [['page'=> 'invalid'], 422],
  123. 'pocket-enabled' => [['enabled'=> true], null],
  124. 'isDashboard-true' => [['isDashboard'=> true], null],
  125. 'location-betweenDiscussions' => [['location'=> 'BetweenDiscussions'], null],
  126. 'location-content' => [['location'=> 'Content'], null],
  127. 'location-betweenComments' => [['location'=> 'BetweenComments'], null],
  128. 'location-head' => [['location'=> 'Head'], null],
  129. 'location-foot' => [['location'=> 'Foot'], null],
  130. 'location-custom' => [['location'=> 'Custom'], null],
  131. 'location-fail' => [['location'=> 'wrongLocation'], 422],
  132. 'pocket-roleIDs' => [['roleIDs'=> 'roleIDs'], null]
  133. //TODO SiteHome avialable with subc
  134. ];
  135. return $r;
  136. }
  137. /**
  138. * Pocket template.
  139. *
  140. * @return array
  141. */
  142. protected function getPocketTemplate(): array {
  143. $rd = rand(10, 1000);
  144. return [
  145. 'name' => $rd.'pocketApiTest',
  146. 'body' => 'pocketApiTest',
  147. 'widgetID' => null,
  148. 'repeatType' => 'once',
  149. 'repeatEvery' => '',
  150. 'repeatIndexes' => null,
  151. 'mobileType' => 'never',
  152. 'roleIDs' => '',
  153. 'isEmbeddable' => false,
  154. 'isAd' => false,
  155. 'page' => 'home',
  156. 'enabled' => false,
  157. 'isDashboard' => false,
  158. 'sort' => 0,
  159. 'location' => 'Panel',
  160. 'categoryID' => null,
  161. 'includeChildCategories' => null,
  162. 'format' => 'raw',
  163. ];
  164. }
  165. /**
  166. * Prepare pockets data.
  167. *
  168. * @param array $fields
  169. * @param int|null $failCode
  170. */
  171. protected function preparePockets(array $fields, $failCode) {
  172. if (array_key_exists('roleIDs', $fields)) {
  173. $roles = RoleModel::roles();
  174. $roleIds = array_column($roles, 'RoleID');
  175. self::$data['roleIDs'] = $roleIds;
  176. $fields['roleIDs'] = $roleIds;
  177. }
  178. $pocketTemplate = $this->getPocketTemplate();
  179. //Update template with provider fields.
  180. foreach ($fields as $key => $value) {
  181. $pocketTemplate[$key] = $value;
  182. }
  183. foreach ($pocketTemplate as $key => $value) {
  184. if ($value === '' || $value === null) {
  185. unset($pocketTemplate[$key]);
  186. }
  187. }
  188. try {
  189. $result = $this->api()->post("/pockets", $pocketTemplate);
  190. if ($result->getBody()['pocketID'] && !isset(self::$data['pocketID'])) {
  191. self::$data['pocketID'] = $result->getBody()['pocketID'];
  192. }
  193. if (empty($failCode)) {
  194. $this->assertEquals(201, $result->getStatusCode());
  195. }
  196. } catch (Exception $e) {
  197. $this->assertEquals($e->getCode(), $failCode);
  198. }
  199. }
  200. /**
  201. * Test Posting pockets.
  202. *
  203. * @param array $fields
  204. * @param int|null $fail
  205. *
  206. * @dataProvider providePocketFields
  207. */
  208. public function testPostPocket(array $fields, ?int $fail) {
  209. $this->preparePockets($fields, $fail);
  210. }
  211. /**
  212. * Test deleting a pocket.
  213. *
  214. * @depends testPostPocket
  215. */
  216. public function testDelete() {
  217. $result = $this->api()->delete("/pockets/".self::$data['pocketID']);
  218. $this->assertEquals(204, $result->getStatusCode());
  219. $this->expectExceptionMessage("Pocket Not Found");
  220. $this->api()->get("/pockets/".self::$data['pocketID']);
  221. }
  222. /**
  223. * {@inheritdoc}
  224. * @requires function MessagesApiController::patch
  225. */
  226. public function testEditFormatCompat(string $editSuffix = "/edit") {
  227. $this->fail(__METHOD__.' needs to be implemented');
  228. }
  229. }