2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests;
|
2016-09-30 00:07:58 +08:00
|
|
|
|
2022-07-13 02:38:11 +08:00
|
|
|
use BookStack\Auth\Permissions\JointPermissionBuilder;
|
2020-03-15 02:29:31 +08:00
|
|
|
use BookStack\Auth\Permissions\RolePermission;
|
|
|
|
use BookStack\Auth\Role;
|
|
|
|
use BookStack\Auth\User;
|
2020-11-22 08:17:45 +08:00
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Chapter;
|
|
|
|
use BookStack\Entities\Models\Page;
|
2021-09-26 22:48:22 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-04-21 04:59:17 +08:00
|
|
|
use Illuminate\Support\Facades\View;
|
2020-03-15 02:29:31 +08:00
|
|
|
|
2021-03-04 06:11:00 +08:00
|
|
|
class PublicActionTest extends TestCase
|
2016-09-30 00:07:58 +08:00
|
|
|
{
|
|
|
|
public function test_app_not_public()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'false']);
|
2021-03-04 06:11:00 +08:00
|
|
|
$book = Book::query()->first();
|
|
|
|
$this->get('/books')->assertRedirect('/login');
|
|
|
|
$this->get($book->getUrl())->assertRedirect('/login');
|
2016-09-30 00:07:58 +08:00
|
|
|
|
2021-03-04 06:11:00 +08:00
|
|
|
$page = Page::query()->first();
|
|
|
|
$this->get($page->getUrl())->assertRedirect('/login');
|
2016-09-30 00:07:58 +08:00
|
|
|
}
|
|
|
|
|
2019-01-05 23:01:16 +08:00
|
|
|
public function test_login_link_visible()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-06-26 23:23:15 +08:00
|
|
|
$this->get('/')->assertElementExists('a[href="' . url('/login') . '"]');
|
2019-01-05 23:01:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_register_link_visible_when_enabled()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-03-04 06:11:00 +08:00
|
|
|
$home = $this->get('/');
|
|
|
|
$home->assertSee(url('/login'));
|
|
|
|
$home->assertDontSee(url('/register'));
|
2019-01-05 23:01:16 +08:00
|
|
|
|
|
|
|
$this->setSettings(['app-public' => 'true', 'registration-enabled' => 'true']);
|
2021-03-04 06:11:00 +08:00
|
|
|
$home = $this->get('/');
|
|
|
|
$home->assertSee(url('/login'));
|
|
|
|
$home->assertSee(url('/register'));
|
2019-01-05 23:01:16 +08:00
|
|
|
}
|
|
|
|
|
2016-09-30 00:07:58 +08:00
|
|
|
public function test_books_viewable()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-03-04 06:11:00 +08:00
|
|
|
$books = Book::query()->orderBy('name', 'asc')->take(10)->get();
|
2016-09-30 00:07:58 +08:00
|
|
|
$bookToVisit = $books[1];
|
|
|
|
|
|
|
|
// Check books index page is showing
|
2021-03-04 06:11:00 +08:00
|
|
|
$resp = $this->get('/books');
|
|
|
|
$resp->assertStatus(200);
|
|
|
|
$resp->assertSee($books[0]->name);
|
|
|
|
|
|
|
|
// Check individual book page is showing and it's child contents are visible.
|
|
|
|
$resp = $this->get($bookToVisit->getUrl());
|
|
|
|
$resp->assertSee($bookToVisit->name);
|
|
|
|
$resp->assertSee($bookToVisit->chapters()->first()->name);
|
2016-09-30 00:07:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_chapters_viewable()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-03-04 06:11:00 +08:00
|
|
|
/** @var Chapter $chapterToVisit */
|
|
|
|
$chapterToVisit = Chapter::query()->first();
|
2016-09-30 00:07:58 +08:00
|
|
|
$pageToVisit = $chapterToVisit->pages()->first();
|
|
|
|
|
|
|
|
// Check chapters index page is showing
|
2021-03-04 06:11:00 +08:00
|
|
|
$resp = $this->get($chapterToVisit->getUrl());
|
|
|
|
$resp->assertStatus(200);
|
|
|
|
$resp->assertSee($chapterToVisit->name);
|
|
|
|
// Check individual chapter page is showing and it's child contents are visible.
|
|
|
|
$resp->assertSee($pageToVisit->name);
|
|
|
|
$resp = $this->get($pageToVisit->getUrl());
|
|
|
|
$resp->assertStatus(200);
|
|
|
|
$resp->assertSee($chapterToVisit->book->name);
|
|
|
|
$resp->assertSee($chapterToVisit->name);
|
2016-09-30 00:07:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_public_page_creation()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2020-03-15 02:29:31 +08:00
|
|
|
$publicRole = Role::getSystemRole('public');
|
2016-09-30 00:07:58 +08:00
|
|
|
// Grant all permissions to public
|
|
|
|
$publicRole->permissions()->detach();
|
2020-03-15 02:29:31 +08:00
|
|
|
foreach (RolePermission::all() as $perm) {
|
2016-09-30 00:07:58 +08:00
|
|
|
$publicRole->attachPermission($perm);
|
|
|
|
}
|
2022-07-13 02:38:11 +08:00
|
|
|
$this->app->make(JointPermissionBuilder::class)->buildJointPermissionForRole($publicRole);
|
2016-09-30 00:07:58 +08:00
|
|
|
|
2021-03-04 06:11:00 +08:00
|
|
|
/** @var Chapter $chapter */
|
|
|
|
$chapter = Chapter::query()->first();
|
|
|
|
$resp = $this->get($chapter->getUrl());
|
|
|
|
$resp->assertSee('New Page');
|
2021-06-26 23:23:15 +08:00
|
|
|
$resp->assertElementExists('a[href="' . $chapter->getUrl('/create-page') . '"]');
|
2016-09-30 00:07:58 +08:00
|
|
|
|
2021-03-04 06:11:00 +08:00
|
|
|
$resp = $this->get($chapter->getUrl('/create-page'));
|
|
|
|
$resp->assertSee('Continue');
|
|
|
|
$resp->assertSee('Page Name');
|
2021-06-26 23:23:15 +08:00
|
|
|
$resp->assertElementExists('form[action="' . $chapter->getUrl('/create-guest-page') . '"]');
|
2021-03-04 06:11:00 +08:00
|
|
|
|
|
|
|
$resp = $this->post($chapter->getUrl('/create-guest-page'), ['name' => 'My guest page']);
|
|
|
|
$resp->assertRedirect($chapter->book->getUrl('/page/my-guest-page/edit'));
|
2016-09-30 00:07:58 +08:00
|
|
|
|
2020-03-15 02:29:31 +08:00
|
|
|
$user = User::getDefault();
|
2021-03-04 06:11:00 +08:00
|
|
|
$this->assertDatabaseHas('pages', [
|
2021-06-26 23:23:15 +08:00
|
|
|
'name' => 'My guest page',
|
2016-09-30 00:07:58 +08:00
|
|
|
'chapter_id' => $chapter->id,
|
|
|
|
'created_by' => $user->id,
|
2021-06-26 23:23:15 +08:00
|
|
|
'updated_by' => $user->id,
|
2016-09-30 00:07:58 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-02-06 05:19:29 +08:00
|
|
|
public function test_content_not_listed_on_404_for_public_users()
|
|
|
|
{
|
2021-03-04 06:11:00 +08:00
|
|
|
$page = Page::query()->first();
|
2021-04-21 04:59:17 +08:00
|
|
|
$page->fill(['name' => 'my testing random unique page name'])->save();
|
2021-03-04 06:11:00 +08:00
|
|
|
$this->asAdmin()->get($page->getUrl()); // Fake visit to show on recents
|
|
|
|
$resp = $this->get('/cats/dogs/hippos');
|
|
|
|
$resp->assertStatus(404);
|
|
|
|
$resp->assertSee($page->name);
|
2021-04-21 04:59:17 +08:00
|
|
|
View::share('pageTitle', '');
|
2021-03-04 06:11:00 +08:00
|
|
|
|
2020-03-15 02:29:31 +08:00
|
|
|
Auth::logout();
|
2021-03-04 06:11:00 +08:00
|
|
|
$resp = $this->get('/cats/dogs/hippos');
|
|
|
|
$resp->assertStatus(404);
|
|
|
|
$resp->assertDontSee($page->name);
|
2017-02-06 05:19:29 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 19:41:40 +08:00
|
|
|
public function test_robots_effected_by_public_status()
|
|
|
|
{
|
2021-03-04 06:11:00 +08:00
|
|
|
$this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /");
|
2018-03-31 19:41:40 +08:00
|
|
|
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
|
|
|
|
2021-03-04 06:11:00 +08:00
|
|
|
$resp = $this->get('/robots.txt');
|
|
|
|
$resp->assertSee("User-agent: *\nDisallow:");
|
2021-06-26 23:23:15 +08:00
|
|
|
$resp->assertDontSee('Disallow: /');
|
2018-03-31 19:41:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_robots_effected_by_setting()
|
|
|
|
{
|
2021-03-04 06:11:00 +08:00
|
|
|
$this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /");
|
2018-03-31 19:41:40 +08:00
|
|
|
|
|
|
|
config()->set('app.allow_robots', true);
|
|
|
|
|
2021-03-04 06:11:00 +08:00
|
|
|
$resp = $this->get('/robots.txt');
|
|
|
|
$resp->assertSee("User-agent: *\nDisallow:");
|
2021-06-26 23:23:15 +08:00
|
|
|
$resp->assertDontSee('Disallow: /');
|
2018-03-31 19:41:40 +08:00
|
|
|
|
|
|
|
// Check config overrides app-public setting
|
|
|
|
config()->set('app.allow_robots', false);
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-03-04 06:11:00 +08:00
|
|
|
$this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /");
|
2018-03-31 19:41:40 +08:00
|
|
|
}
|
|
|
|
|
2020-03-15 02:29:31 +08:00
|
|
|
public function test_public_view_then_login_redirects_to_previous_content()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-03-04 06:11:00 +08:00
|
|
|
/** @var Book $book */
|
2020-03-15 02:29:31 +08:00
|
|
|
$book = Book::query()->first();
|
2021-03-04 06:11:00 +08:00
|
|
|
$resp = $this->get($book->getUrl());
|
|
|
|
$resp->assertSee($book->name);
|
|
|
|
|
|
|
|
$this->get('/login');
|
|
|
|
$resp = $this->post('/login', ['email' => 'admin@admin.com', 'password' => 'password']);
|
|
|
|
$resp->assertRedirect($book->getUrl());
|
2020-03-15 02:29:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_access_hidden_content_then_login_redirects_to_intended_content()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-03-04 06:11:00 +08:00
|
|
|
/** @var Book $book */
|
2020-03-15 02:29:31 +08:00
|
|
|
$book = Book::query()->first();
|
|
|
|
$this->setEntityRestrictions($book);
|
|
|
|
|
2021-03-04 06:11:00 +08:00
|
|
|
$resp = $this->get($book->getUrl());
|
|
|
|
$resp->assertSee('Book not found');
|
|
|
|
|
|
|
|
$this->get('/login');
|
|
|
|
$resp = $this->post('/login', ['email' => 'admin@admin.com', 'password' => 'password']);
|
|
|
|
$resp->assertRedirect($book->getUrl());
|
|
|
|
$this->followRedirects($resp)->assertSee($book->name);
|
2020-03-15 02:29:31 +08:00
|
|
|
}
|
2021-06-26 23:23:15 +08:00
|
|
|
}
|