2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests;
|
2016-09-29 12:07:58 -04:00
|
|
|
|
2020-03-14 14:29:31 -04:00
|
|
|
use BookStack\Auth\Permissions\RolePermission;
|
|
|
|
use BookStack\Auth\Role;
|
|
|
|
use BookStack\Auth\User;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Chapter;
|
2021-09-26 10:48:22 -04:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-04-20 16:59:17 -04:00
|
|
|
use Illuminate\Support\Facades\View;
|
2020-03-14 14:29:31 -04:00
|
|
|
|
2021-03-03 17:11:00 -05:00
|
|
|
class PublicActionTest extends TestCase
|
2016-09-29 12:07:58 -04:00
|
|
|
{
|
|
|
|
public function test_app_not_public()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'false']);
|
2022-09-29 12:31:38 -04:00
|
|
|
$book = $this->entities->book();
|
2021-03-03 17:11:00 -05:00
|
|
|
$this->get('/books')->assertRedirect('/login');
|
|
|
|
$this->get($book->getUrl())->assertRedirect('/login');
|
2016-09-29 12:07:58 -04:00
|
|
|
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2021-03-03 17:11:00 -05:00
|
|
|
$this->get($page->getUrl())->assertRedirect('/login');
|
2016-09-29 12:07:58 -04:00
|
|
|
}
|
|
|
|
|
2019-01-05 10:01:16 -05:00
|
|
|
public function test_login_link_visible()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2022-07-23 10:10:18 -04:00
|
|
|
$resp = $this->get('/');
|
|
|
|
$this->withHtml($resp)->assertElementExists('a[href="' . url('/login') . '"]');
|
2019-01-05 10:01:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_register_link_visible_when_enabled()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-03-03 17:11:00 -05:00
|
|
|
$home = $this->get('/');
|
|
|
|
$home->assertSee(url('/login'));
|
|
|
|
$home->assertDontSee(url('/register'));
|
2019-01-05 10:01:16 -05:00
|
|
|
|
|
|
|
$this->setSettings(['app-public' => 'true', 'registration-enabled' => 'true']);
|
2021-03-03 17:11:00 -05:00
|
|
|
$home = $this->get('/');
|
|
|
|
$home->assertSee(url('/login'));
|
|
|
|
$home->assertSee(url('/register'));
|
2019-01-05 10:01:16 -05:00
|
|
|
}
|
|
|
|
|
2016-09-29 12:07:58 -04:00
|
|
|
public function test_books_viewable()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-03-03 17:11:00 -05:00
|
|
|
$books = Book::query()->orderBy('name', 'asc')->take(10)->get();
|
2016-09-29 12:07:58 -04:00
|
|
|
$bookToVisit = $books[1];
|
|
|
|
|
|
|
|
// Check books index page is showing
|
2021-03-03 17:11:00 -05: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-29 12:07:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_chapters_viewable()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-03-03 17:11:00 -05:00
|
|
|
/** @var Chapter $chapterToVisit */
|
|
|
|
$chapterToVisit = Chapter::query()->first();
|
2016-09-29 12:07:58 -04:00
|
|
|
$pageToVisit = $chapterToVisit->pages()->first();
|
|
|
|
|
|
|
|
// Check chapters index page is showing
|
2021-03-03 17:11:00 -05: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-29 12:07:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_public_page_creation()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2020-03-14 14:29:31 -04:00
|
|
|
$publicRole = Role::getSystemRole('public');
|
2016-09-29 12:07:58 -04:00
|
|
|
// Grant all permissions to public
|
|
|
|
$publicRole->permissions()->detach();
|
2020-03-14 14:29:31 -04:00
|
|
|
foreach (RolePermission::all() as $perm) {
|
2016-09-29 12:07:58 -04:00
|
|
|
$publicRole->attachPermission($perm);
|
|
|
|
}
|
2022-07-16 16:50:42 -04:00
|
|
|
user()->clearPermissionCache();
|
2016-09-29 12:07:58 -04:00
|
|
|
|
2022-09-29 12:31:38 -04:00
|
|
|
$chapter = $this->entities->chapter();
|
2021-03-03 17:11:00 -05:00
|
|
|
$resp = $this->get($chapter->getUrl());
|
|
|
|
$resp->assertSee('New Page');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('a[href="' . $chapter->getUrl('/create-page') . '"]');
|
2016-09-29 12:07:58 -04:00
|
|
|
|
2021-03-03 17:11:00 -05:00
|
|
|
$resp = $this->get($chapter->getUrl('/create-page'));
|
|
|
|
$resp->assertSee('Continue');
|
|
|
|
$resp->assertSee('Page Name');
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('form[action="' . $chapter->getUrl('/create-guest-page') . '"]');
|
2021-03-03 17:11:00 -05: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-29 12:07:58 -04:00
|
|
|
|
2020-03-14 14:29:31 -04:00
|
|
|
$user = User::getDefault();
|
2021-03-03 17:11:00 -05:00
|
|
|
$this->assertDatabaseHas('pages', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'name' => 'My guest page',
|
2016-09-29 12:07:58 -04:00
|
|
|
'chapter_id' => $chapter->id,
|
|
|
|
'created_by' => $user->id,
|
2021-06-26 11:23:15 -04:00
|
|
|
'updated_by' => $user->id,
|
2016-09-29 12:07:58 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-02-05 16:19:29 -05:00
|
|
|
public function test_content_not_listed_on_404_for_public_users()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2021-04-20 16:59:17 -04:00
|
|
|
$page->fill(['name' => 'my testing random unique page name'])->save();
|
2021-03-03 17:11:00 -05: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-20 16:59:17 -04:00
|
|
|
View::share('pageTitle', '');
|
2021-03-03 17:11:00 -05:00
|
|
|
|
2020-03-14 14:29:31 -04:00
|
|
|
Auth::logout();
|
2021-03-03 17:11:00 -05:00
|
|
|
$resp = $this->get('/cats/dogs/hippos');
|
|
|
|
$resp->assertStatus(404);
|
|
|
|
$resp->assertDontSee($page->name);
|
2017-02-05 16:19:29 -05:00
|
|
|
}
|
|
|
|
|
2018-03-31 07:41:40 -04:00
|
|
|
public function test_robots_effected_by_public_status()
|
|
|
|
{
|
2021-03-03 17:11:00 -05:00
|
|
|
$this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /");
|
2018-03-31 07:41:40 -04:00
|
|
|
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
|
|
|
|
2021-03-03 17:11:00 -05:00
|
|
|
$resp = $this->get('/robots.txt');
|
|
|
|
$resp->assertSee("User-agent: *\nDisallow:");
|
2021-06-26 11:23:15 -04:00
|
|
|
$resp->assertDontSee('Disallow: /');
|
2018-03-31 07:41:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_robots_effected_by_setting()
|
|
|
|
{
|
2021-03-03 17:11:00 -05:00
|
|
|
$this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /");
|
2018-03-31 07:41:40 -04:00
|
|
|
|
|
|
|
config()->set('app.allow_robots', true);
|
|
|
|
|
2021-03-03 17:11:00 -05:00
|
|
|
$resp = $this->get('/robots.txt');
|
|
|
|
$resp->assertSee("User-agent: *\nDisallow:");
|
2021-06-26 11:23:15 -04:00
|
|
|
$resp->assertDontSee('Disallow: /');
|
2018-03-31 07:41:40 -04:00
|
|
|
|
|
|
|
// Check config overrides app-public setting
|
|
|
|
config()->set('app.allow_robots', false);
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2021-03-03 17:11:00 -05:00
|
|
|
$this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /");
|
2018-03-31 07:41:40 -04:00
|
|
|
}
|
|
|
|
|
2020-03-14 14:29:31 -04:00
|
|
|
public function test_public_view_then_login_redirects_to_previous_content()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2022-09-29 12:31:38 -04:00
|
|
|
$book = $this->entities->book();
|
2021-03-03 17:11:00 -05: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-14 14:29:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_access_hidden_content_then_login_redirects_to_intended_content()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-public' => 'true']);
|
2022-09-29 12:31:38 -04:00
|
|
|
$book = $this->entities->book();
|
2023-01-21 06:08:34 -05:00
|
|
|
$this->permissions->setEntityPermissions($book);
|
2020-03-14 14:29:31 -04:00
|
|
|
|
2021-03-03 17:11:00 -05: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-14 14:29:31 -04:00
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
}
|