API Utility Test Traits - HL Vanilla Community
<main> <article class="userContent"> <div class="embedExternal embedImage display-large float-none"> <div class="embedExternal-content"> <a class="embedImage-link" href="https://us.v-cdn.net/6030677/uploads/FO1NKP9UWHQC/microsoftteams-image-288-29.png" rel="nofollow noreferrer noopener ugc" target="_blank"> <img class="embedImage-img" src="https://us.v-cdn.net/6030677/uploads/FO1NKP9UWHQC/microsoftteams-image-288-29.png" alt="MicrosoftTeams-image (8).png" height="108" width="1356" loading="lazy" data-display-size="large" data-float="none"></img></a> </div> </div> <p>Vanilla has various traits for easily creating and testing data through API endpoints. These generally have a suffix <code class="code codeInline" spellcheck="false" tabindex="0">ApiTestTrait</code>. The expect to be used on test cases that use <code class="code codeInline" spellcheck="false" tabindex="0">SiteTestTrait</code> or <code class="code codeInline" spellcheck="false" tabindex="0">AbstractApiV2Test</code>.</p><h2 data-id="structure-of-the-traits">Structure of the Traits</h2><p>The traits generally all have the following in common.</p><ul><li>Easy <code class="code codeInline" spellcheck="false" tabindex="0">createThing()</code> methods for common resources.</li><li>All parameters are always defaulted.</li><li>Any parameter can be overridden by passing an array of overrides. For example <code class="code codeInline" spellcheck="false" tabindex="0">createCategory([ 'name' => 'myName' ])</code>;</li><li>Parameters that have a unique constraint will have a new unique default value for every record created.</li><li>When records have a parent -> child relation ship or dependency, the default parent/relationship will be automatically configured. For example, when creating a discussion, it will automatically be created in the last created category.</li></ul><h2 data-id="available-traits">Available Traits</h2><h3 data-id="communityapitesttrait"><code class="code codeInline" spellcheck="false" tabindex="0">CommunityApiTestTrait</code></h3><ul><li><code class="code codeInline" spellcheck="false" tabindex="0">createCategory()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">createDiscussion()</code> </li><li><code class="code codeInline" spellcheck="false" tabindex="0">createComments()</code> </li><li><code class="code codeInline" spellcheck="false" tabindex="0">createPermissionedCategory($overrides, $viewRoleIDs)</code> - Create a category that is only viewable by to the passed roleIDs.</li></ul><p><strong><em>Note</em></strong>: <strong><em>Please do not use the deprecated </em></strong><strong><code class="code codeInline" spellcheck="false" tabindex="0">CategoryAndDiscussionApiTestTrait</code></strong><strong>.</strong></p><h3 data-id="usersandrolesapitesttrait"><code class="code codeInline" spellcheck="false" tabindex="0">UsersAndRolesApiTestTrait</code></h3><ul><li><code class="code codeInline" spellcheck="false" tabindex="0">createUser()</code> </li><li><code class="code codeInline" spellcheck="false" tabindex="0">updateUser()</code> </li><li><code class="code codeInline" spellcheck="false" tabindex="0">createRole()</code> </li><li><code class="code codeInline" spellcheck="false" tabindex="0">runWithPermissions($callable, ...$permissions)</code> - Run some callback with a user that has the given permissions. Supports global, and resource specific permissions.</li></ul><h2 data-id="cloud-only-trait">Cloud Only Trait</h2><p>The following traits are only available with access to some Vanilla Cloud only plugins.</p><h3 data-id="groupsandeventsapitesttrait"><code class="code codeInline" spellcheck="false" tabindex="0">GroupsAndEventsApiTestTrait</code></h3><ul><li><code class="code codeInline" spellcheck="false" tabindex="0">createGroup()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">inviteGroup($userIDs)</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">joinGroup()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">createEventCategory()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">createGroupDiscussion()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">createEvent()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">createGroupHydrated()</code> - Create a group with some content in it by default.</li><li><code class="code codeInline" spellcheck="false" tabindex="0">clearEvents()</code> - Delete events from a category or group.</li></ul><h3 data-id="kbapitesttrait"><code class="code codeInline" spellcheck="false" tabindex="0">KbApiTestTrait</code></h3><ul><li><code class="code codeInline" spellcheck="false" tabindex="0">createKnowledgeBase()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">createKnowledgeCategory()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">createArticle()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">translateKnowledgeBase()</code> - Translate a knowledge base into some preset languages.</li></ul><h3 data-id="ranksapitesttrait"><code class="code codeInline" spellcheck="false" tabindex="0">RanksApiTestTrait</code></h3><ul><li><code class="code codeInline" spellcheck="false" tabindex="0">createRank()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">rankUser()</code></li></ul><h3 data-id="subcommunitytesttrait"><code class="code codeInline" spellcheck="false" tabindex="0">SubcommunityTestTrait</code></h3><ul><li><code class="code codeInline" spellcheck="false" tabindex="0">createSubcommunity()</code></li><li><code class="code codeInline" spellcheck="false" tabindex="0">createProduct()</code></li></ul><p><br></p> </article> </main>