Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Testing/Concerns/ElevatesSessions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Statamic\Testing\Concerns;

use Carbon\Carbon;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Testing\TestResponse;

trait ElevatesSessions
{
protected function actingAsWithElevatedSession(Authenticatable $user, ?Carbon $time = null)
{
return $this->actingAs($user)->withElevatedSession($time);
}

protected function withElevatedSession(?Carbon $time = null)
{
return $this->session(['statamic_elevated_session' => ($time ?? now())->timestamp]);
}

protected function addElevatedSessionMacros()
{
TestResponse::macro('assertRedirectToConfirmPasswordForElevatedSession', function () {
return $this->assertRedirect(route('statamic.cp.confirm-password'));
});

TestResponse::macro('assertElevatedSessionRequiredJsonResponse', function () {
return $this->assertJson(['message' => 'Requires an elevated session.'])->assertStatus(403);
});
}
}
56 changes: 56 additions & 0 deletions src/Testing/Concerns/FakesRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Statamic\Testing\Concerns;

use Illuminate\Support\Collection;
use Statamic\Auth\File\Role as FileRole;
use Statamic\Auth\File\RoleRepository;
use Statamic\Contracts\Auth\RoleRepository as RepositoryContract;
use Statamic\Facades\Role;

trait FakesRoles
{
protected function setTestRoles($roles)
{
$roles = collect($roles)
->mapWithKeys(function ($permissions, $handle) {
$handle = is_string($permissions) ? $permissions : $handle;
$permissions = is_string($permissions) ? [] : $permissions;

return [$handle => $permissions];
})
->map(function ($permissions, $handle) {
return $permissions instanceof FileRole
? $permissions->handle($handle)
: Role::make()->handle($handle)->addPermission($permissions);
});

$fake = new class($roles) extends RoleRepository
{
protected $roles;

public function __construct($roles)
{
$this->roles = $roles;
}

public function all(): Collection
{
return $this->roles;
}
};

app()->instance(RepositoryContract::class, $fake);
Role::swap($fake);
}

protected function setTestRole(string $handle, array $permissions)
{
$this->setTestRoles(array_merge(
Role::all()->mapWithKeys(function ($role) {
return [$role->handle() => $role->permissions()];
})->toArray(),
[$handle => $permissions]
));
}
}
46 changes: 46 additions & 0 deletions src/Testing/Concerns/FakesUserGroups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Statamic\Testing\Concerns;

use Illuminate\Support\Collection;
use Statamic\Auth\File\UserGroup as FileUserGroup;
use Statamic\Auth\File\UserGroupRepository;
use Statamic\Contracts\Auth\UserGroupRepository as RepositoryContract;
use Statamic\Facades\UserGroup;

trait FakesUserGroups
{
protected function setTestUserGroups($groups)
{
$groups = collect($groups)
->mapWithKeys(function ($roles, $handle) {
$handle = is_string($roles) ? $roles : $handle;
$roles = is_string($roles) ? [] : $roles;

return [$handle => $roles];
})
->map(function ($roles, $handle) {
return $roles instanceof FileUserGroup
? $roles->handle($handle)
: UserGroup::make()->handle($handle)->roles($roles);
});

$fake = new class($groups) extends UserGroupRepository
{
protected $groups;

public function __construct($groups)
{
$this->groups = $groups;
}

public function all(): Collection
{
return $this->groups;
}
};

app()->instance(RepositoryContract::class, $fake);
UserGroup::swap($fake);
}
}
61 changes: 61 additions & 0 deletions src/Testing/Concerns/FakesViews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Statamic\Testing\Concerns;

use Statamic\Testing\FakeViewEngine;
use Statamic\Testing\FakeViewFactory;
use Statamic\Testing\FakeViewFinder;

trait FakesViews
{
protected $fakeView;
protected $fakeViewFinder;
protected $fakeViewFactory;

public function withFakeViews()
{
$originalFactory = $this->app['view'];

$this->fakeView = app(FakeViewEngine::class);
$this->fakeViewFinder = new FakeViewFinder($this->app['files'], config('view.paths'));

$this->fakeViewFactory = new FakeViewFactory($this->app['view.engine.resolver'], $this->fakeViewFinder, $this->app['events']);
$this->fakeViewFactory->setFakeEngine($this->fakeView);
foreach (array_reverse($originalFactory->getExtensions()) as $ext => $engine) {
$this->fakeViewFactory->addExtension($ext, $engine);
}

$this->app->instance('view.finder', $this->fakeViewFinder);
$this->app->instance('view', $this->fakeViewFactory);
}

public function withStandardFakeViews()
{
$this->withFakeViews();

$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('default', '{{ content }}');
}

public function withStandardFakeErrorViews()
{
$this->withFakeViews();

$this->viewShouldReturnRaw('errors.layout', '{{ template_content }}');
$this->viewShouldReturnRaw('errors.404', 'The 404 template.');
}

public function viewShouldReturnRaw($view, $contents, $extension = 'antlers.html')
{
$this->fakeView->rawContents["$view.$extension"] = $contents;
$this->fakeViewFinder->fakeViews[$view] = $view;
$this->fakeViewFactory->fileExtensions[$view] = $extension;
}

public function viewShouldReturnRendered($view, $contents, $extension = 'antlers.html')
{
$this->fakeView->renderedContents["$view.$extension"] = $contents;
$this->fakeViewFinder->fakeViews[$view] = $view;
$this->fakeViewFactory->fileExtensions[$view] = $extension;
}
}
40 changes: 40 additions & 0 deletions src/Testing/FakeViewEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Statamic\Testing;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Str;
use Statamic\View\Antlers\Engine;

class FakeViewEngine extends Engine
{
public $rawContents = [];
public $renderedContents = [];

public function get($path, array $data = [])
{
if (isset($this->renderedContents[$path])) {
return $this->renderedContents[$path];
}

if (Str::endsWith($path, '.blade.php')) {
return Blade::render($this->rawContents[$path], $data);
}

return parent::get($path, $data);
}

protected function getContents($path)
{
if (isset($this->rawContents[$path])) {
return $this->rawContents[$path];
}

return parent::getContents($path);
}

public function exists($path)
{
return app('view.finder')->exists($path);
}
}
36 changes: 36 additions & 0 deletions src/Testing/FakeViewFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Statamic\Testing;

use Illuminate\View\Factory;
use Illuminate\View\View;

class FakeViewFactory extends Factory
{
public $fileExtensions = [];

protected $fakeEngine;

public function setFakeEngine(FakeViewEngine $engine)
{
$this->fakeEngine = $engine;

return $this;
}

public function make($view, $data = [], $mergeData = [])
{
$ext = $this->fileExtensions[$view] ?? 'antlers.html';

if ($this->fakeEngine->exists($view)) {
return new View($this, $this->fakeEngine, $view, "{$view}.{$ext}", $data);
}

return parent::make($view, $data, $mergeData);
}

public function exists($view)
{
return $this->fakeEngine->exists($view);
}
}
24 changes: 24 additions & 0 deletions src/Testing/FakeViewFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Statamic\Testing;

use Illuminate\View\FileViewFinder;

class FakeViewFinder extends FileViewFinder
{
public $fakeViews = [];

public function find($view)
{
if (isset($this->fakeViews[$view])) {
return $this->fakeViews[$view];
}

return parent::find($view);
}

public function exists($path)
{
return isset($this->fakeViews[$path]);
}
}
25 changes: 2 additions & 23 deletions tests/ElevatesSessions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,9 @@

namespace Tests;

use Carbon\Carbon;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Testing\TestResponse;
use Statamic\Testing\Concerns\ElevatesSessions as BaseElevatesSessions;

trait ElevatesSessions
{
protected function actingAsWithElevatedSession(Authenticatable $user, ?Carbon $time = null)
{
return $this->actingAs($user)->withElevatedSession($time);
}

protected function withElevatedSession(?Carbon $time = null)
{
return $this->session(['statamic_elevated_session' => ($time ?? now())->timestamp]);
}

protected function addElevatedSessionMacros()
{
TestResponse::macro('assertRedirectToConfirmPasswordForElevatedSession', function () {
return $this->assertRedirect(route('statamic.cp.confirm-password'));
});

TestResponse::macro('assertElevatedSessionRequiredJsonResponse', function () {
return $this->assertJson(['message' => 'Requires an elevated session.'])->assertStatus(403);
});
}
use BaseElevatesSessions;
}
50 changes: 2 additions & 48 deletions tests/FakesRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,9 @@

namespace Tests;

use Illuminate\Support\Collection;
use Statamic\Auth\File\Role as FileRole;
use Statamic\Auth\File\RoleRepository;
use Statamic\Contracts\Auth\RoleRepository as RepositoryContract;
use Statamic\Facades\Role;
use Statamic\Testing\Concerns\FakesRoles as BaseFakesRoles;

trait FakesRoles
{
protected function setTestRoles($roles)
{
$roles = collect($roles)
->mapWithKeys(function ($permissions, $handle) {
$handle = is_string($permissions) ? $permissions : $handle;
$permissions = is_string($permissions) ? [] : $permissions;

return [$handle => $permissions];
})
->map(function ($permissions, $handle) {
return $permissions instanceof FileRole
? $permissions->handle($handle)
: Role::make()->handle($handle)->addPermission($permissions);
});

$fake = new class($roles) extends RoleRepository
{
protected $roles;

public function __construct($roles)
{
$this->roles = $roles;
}

public function all(): Collection
{
return $this->roles;
}
};

app()->instance(RepositoryContract::class, $fake);
Role::swap($fake);
}

protected function setTestRole(string $handle, array $permissions)
{
$this->setTestRoles(array_merge(
Role::all()->mapWithKeys(function ($role) {
return [$role->handle() => $role->permissions()];
})->toArray(),
[$handle => $permissions]
));
}
use BaseFakesRoles;
}
Loading
Loading