Skip to content

Instantly share code, notes, and snippets.

@bencroker
Last active May 16, 2023 23:56
Show Gist options
  • Save bencroker/a06f615fc85dad7dacbc1785848a6170 to your computer and use it in GitHub Desktop.
Save bencroker/a06f615fc85dad7dacbc1785848a6170 to your computer and use it in GitHub Desktop.
Sample Craft Pest tests.
<?php
/**
* Sample Craft Pest tests.
*
* @copyright Copyright (c) PutYourLightsOn
*/
use markhuot\craftpest\factories\Entry;
use markhuot\craftpest\factories\User;
it('shows the most recent course on the course page', function() {
$course = Entry::factory()
->title('Using Craft Pest')
->section('courses')
->create();
$this->get('/course')
->assertOk()
->assertSee($course->title);
});
it('shows similar courses in the sidebar', function() {
$similarCourse = Entry::factory()
->section('courses')
->title('A Related Course')
->create();
$course = Entry::factory()
->section('courses')
->set('similarTo', [$similarCourse->id])
->create();
$this->get($course->uri)
->assertOk()
->expectSelector('.sidebar')
->text->toContain($similarCourse->title);
});
it('marks a video as complete', function() {
$video = Entry::factory()
->section('videos')
->create();
$this->get($video->uri)
->assertOk()
->querySelector('.mark-as-complete')
->click()
->assertSee('You have completed the video');
});
it('prompt guests viewing a video page to login', function() {
$video = Entry::factory()
->section('videos')
->set('membersOnly', true)
->create();
$this->get($video->uri)
->assertOk()
->assertSee('You must be logged in to watch this video');
});
it('allows users in the pro plan only to watch pro videos', function() {
$video = Entry::factory()
->section('videos')
->set('proMembersOnly', true)
->create();
$freeUser = User::factory()->create();
Craft::$app->users->assignUserToGroups($freeUser->id, [1]);
$this->actingAs($freeUser)
->get($video->uri)
->assertOk()
->assertSee('This video is available to pro users only');
$proUser = User::factory()->create();
Craft::$app->users->assignUserToGroups($proUser->id, [2]);
$this->actingAs($proUser)
->get($video->uri)
->assertOk()
->assertSee('Click to play the video');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment