/tests/CategoryAndDiscussionApiTestTrait.php

http://github.com/vanillaforums/Garden · PHP · 120 lines · 59 code · 20 blank · 41 comment · 2 complexity · 7d6e133ff848d40ae0c3be1fea780aa7 MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright 2009-2020 Vanilla Forums Inc.
  4. * @license Proprietary
  5. */
  6. namespace VanillaTests;
  7. use Gdn;
  8. use TagModel;
  9. /**
  10. * Class CategoryAndDiscussionApiTestTrait
  11. *
  12. * @package VanillaTests
  13. *
  14. * @deprecated Use CommunityApiTestTrait instead.
  15. */
  16. trait CategoryAndDiscussionApiTestTrait {
  17. /**
  18. * @var int|null $lastInsertedCategoryID
  19. */
  20. protected $lastInsertedCategoryID;
  21. /**
  22. * @var int|null $lastInsertedDiscussionID
  23. */
  24. protected $lastInsertedDiscussionID;
  25. /**
  26. * Clear local info between tests.
  27. */
  28. public function setUpCategoryAndDiscussionApiTestTrait(): void {
  29. $this->lastInsertedCategoryID = null;
  30. $this->lastInsertedDiscussionID = null;
  31. \DiscussionModel::cleanForTests();
  32. }
  33. /**
  34. * Create a test category.
  35. *
  36. * @param array $overrides
  37. * @return array
  38. */
  39. public function createCategory(array $overrides = []): array {
  40. /** @var \CategoriesApiController $categoriesAPIController */
  41. $categoriesAPIController = Gdn::getContainer()->get('CategoriesApiController');
  42. $categoryInfo = uniqid('category_');
  43. $body = $overrides +
  44. [
  45. 'name' => $categoryInfo,
  46. 'urlCode' => $categoryInfo,
  47. ];
  48. $category = $categoriesAPIController->post($body);
  49. $this->lastInsertedCategoryID = $category['categoryID'];
  50. return $category;
  51. }
  52. /**
  53. * Create a Discussion.
  54. *
  55. * @param array $overrides
  56. * @return array
  57. */
  58. public function createDiscussion(array $overrides = []): array {
  59. /** @var \DiscussionsApiController $discussionAPIController */
  60. $discussionAPIController = Gdn::getContainer()->get('DiscussionsApiController');
  61. $categoryID = $overrides['categoryID'] ?? $this->lastInsertedCategoryID;
  62. if (!$categoryID) {
  63. $category = $this->createCategory();
  64. $categoryID = $category['categoryID'];
  65. }
  66. $body = VanillaTestCase::sprintfCounter($overrides +
  67. [
  68. 'categoryID' => $categoryID,
  69. 'type' => null,
  70. 'name' => 'Discussion %s',
  71. 'body' => 'Discussion %s',
  72. 'format' => 'markdown',
  73. ]);
  74. $discussion = $discussionAPIController->post($body)->getData();
  75. $this->lastInsertedDiscussionID = $discussion['discussionID'];
  76. return $discussion;
  77. }
  78. /**
  79. * Create Tags.
  80. *
  81. * @param array $overrides
  82. * @return array
  83. */
  84. public function createTag(array $overrides = []): array {
  85. /** @var TagModel $tagModel */
  86. $tagModel = Gdn::getContainer()->get(TagModel::class);
  87. $name = $overrides['name'] ?? uniqid('tagName_');
  88. $fullName = $overrides['fullName'] ?? $name;
  89. $type = $overrides['type'] ?? '';
  90. $tag = $tagModel->save([
  91. 'Name' => $name,
  92. 'FullName' => $fullName,
  93. 'Type' => $type
  94. ]);
  95. if (!is_array($tag)) {
  96. $tag = $tagModel->getWhere(["Name" => $name])->firstRow(DATASET_TYPE_ARRAY);
  97. }
  98. return $tag;
  99. }
  100. }