mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-31 15:24:31 +03:00
Added further attribute endpoints and added tests
This commit is contained in:
@ -181,7 +181,7 @@ class AuthTest extends TestCase
|
||||
public function test_user_deletion()
|
||||
{
|
||||
$userDetails = factory(\BookStack\User::class)->make();
|
||||
$user = $this->getNewUser($userDetails->toArray());
|
||||
$user = $this->getEditor($userDetails->toArray());
|
||||
|
||||
$this->asAdmin()
|
||||
->visit('/settings/users/' . $user->id)
|
||||
|
115
tests/Entity/AttributeTests.php
Normal file
115
tests/Entity/AttributeTests.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php namespace Entity;
|
||||
|
||||
use BookStack\Attribute;
|
||||
use BookStack\Page;
|
||||
use BookStack\Services\PermissionService;
|
||||
|
||||
class AttributeTests extends \TestCase
|
||||
{
|
||||
|
||||
protected $defaultAttrCount = 20;
|
||||
|
||||
/**
|
||||
* Get an instance of a page that has many attributes.
|
||||
* @param Attribute[]|bool $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getPageWithAttributes($attributes = false)
|
||||
{
|
||||
$page = Page::first();
|
||||
|
||||
if (!$attributes) {
|
||||
$attributes = factory(Attribute::class, $this->defaultAttrCount)->make();
|
||||
}
|
||||
|
||||
$page->attributes()->saveMany($attributes);
|
||||
return $page;
|
||||
}
|
||||
|
||||
public function test_get_page_attributes()
|
||||
{
|
||||
$page = $this->getPageWithAttributes();
|
||||
|
||||
// Add some other attributes to check they don't interfere
|
||||
factory(Attribute::class, $this->defaultAttrCount)->create();
|
||||
|
||||
$this->asAdmin()->get("/ajax/attributes/get/page/" . $page->id)
|
||||
->shouldReturnJson();
|
||||
|
||||
$json = json_decode($this->response->getContent());
|
||||
$this->assertTrue(count($json) === $this->defaultAttrCount, "Returned JSON item count is not as expected");
|
||||
}
|
||||
|
||||
public function test_attribute_name_suggestions()
|
||||
{
|
||||
// Create some attributes with similar names to test with
|
||||
$attrs = collect();
|
||||
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'country']));
|
||||
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'color']));
|
||||
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'city']));
|
||||
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'county']));
|
||||
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'planet']));
|
||||
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'plans']));
|
||||
$page = $this->getPageWithAttributes($attrs);
|
||||
|
||||
$this->asAdmin()->get('/ajax/attributes/suggest?search=dog')->seeJsonEquals([]);
|
||||
$this->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country', 'county']);
|
||||
$this->get('/ajax/attributes/suggest?search=cou')->seeJsonEquals(['country', 'county']);
|
||||
$this->get('/ajax/attributes/suggest?search=pla')->seeJsonEquals(['planet', 'plans']);
|
||||
}
|
||||
|
||||
public function test_entity_permissions_effect_attribute_suggestions()
|
||||
{
|
||||
$permissionService = $this->app->make(PermissionService::class);
|
||||
|
||||
// Create some attributes with similar names to test with and save to a page
|
||||
$attrs = collect();
|
||||
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'country']));
|
||||
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'color']));
|
||||
$page = $this->getPageWithAttributes($attrs);
|
||||
|
||||
$this->asAdmin()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
|
||||
$this->asEditor()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
|
||||
|
||||
// Set restricted permission the page
|
||||
$page->restricted = true;
|
||||
$page->save();
|
||||
$permissionService->buildJointPermissionsForEntity($page);
|
||||
|
||||
$this->asAdmin()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
|
||||
$this->asEditor()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals([]);
|
||||
}
|
||||
|
||||
public function test_entity_attribute_updating()
|
||||
{
|
||||
$page = $this->getPageWithAttributes();
|
||||
|
||||
$testJsonData = [
|
||||
['name' => 'color', 'value' => 'red'],
|
||||
['name' => 'color', 'value' => ' blue '],
|
||||
['name' => 'city', 'value' => 'London '],
|
||||
['name' => 'country', 'value' => ' England'],
|
||||
];
|
||||
$testResponseJsonData = [
|
||||
['name' => 'color', 'value' => 'red'],
|
||||
['name' => 'color', 'value' => 'blue'],
|
||||
['name' => 'city', 'value' => 'London'],
|
||||
['name' => 'country', 'value' => 'England'],
|
||||
];
|
||||
|
||||
$this->asAdmin()->json("POST", "/ajax/attributes/update/page/" . $page->id, ['attributes' => $testJsonData]);
|
||||
$this->asAdmin()->get("/ajax/attributes/get/page/" . $page->id);
|
||||
$jsonData = json_decode($this->response->getContent());
|
||||
// Check counts
|
||||
$this->assertTrue(count($jsonData) === count($testJsonData), "The received attribute count is incorrect");
|
||||
// Check data is correct
|
||||
$testDataCorrect = true;
|
||||
foreach ($jsonData as $data) {
|
||||
$testItem = ['name' => $data->name, 'value' => $data->value];
|
||||
if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
|
||||
}
|
||||
$testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
|
||||
$this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($jsonData)));
|
||||
}
|
||||
|
||||
}
|
@ -161,8 +161,8 @@ class EntityTest extends TestCase
|
||||
public function test_entities_viewable_after_creator_deletion()
|
||||
{
|
||||
// Create required assets and revisions
|
||||
$creator = $this->getNewUser();
|
||||
$updater = $this->getNewUser();
|
||||
$creator = $this->getEditor();
|
||||
$updater = $this->getEditor();
|
||||
$entities = $this->createEntityChainBelongingToUser($creator, $updater);
|
||||
$this->actingAs($creator);
|
||||
app('BookStack\Repos\UserRepo')->destroy($creator);
|
||||
@ -174,8 +174,8 @@ class EntityTest extends TestCase
|
||||
public function test_entities_viewable_after_updater_deletion()
|
||||
{
|
||||
// Create required assets and revisions
|
||||
$creator = $this->getNewUser();
|
||||
$updater = $this->getNewUser();
|
||||
$creator = $this->getEditor();
|
||||
$updater = $this->getEditor();
|
||||
$entities = $this->createEntityChainBelongingToUser($creator, $updater);
|
||||
$this->actingAs($updater);
|
||||
app('BookStack\Repos\UserRepo')->destroy($updater);
|
||||
@ -198,7 +198,7 @@ class EntityTest extends TestCase
|
||||
|
||||
public function test_recently_created_pages_view()
|
||||
{
|
||||
$user = $this->getNewUser();
|
||||
$user = $this->getEditor();
|
||||
$content = $this->createEntityChainBelongingToUser($user);
|
||||
|
||||
$this->asAdmin()->visit('/pages/recently-created')
|
||||
@ -207,7 +207,7 @@ class EntityTest extends TestCase
|
||||
|
||||
public function test_recently_updated_pages_view()
|
||||
{
|
||||
$user = $this->getNewUser();
|
||||
$user = $this->getEditor();
|
||||
$content = $this->createEntityChainBelongingToUser($user);
|
||||
|
||||
$this->asAdmin()->visit('/pages/recently-updated')
|
||||
@ -241,7 +241,7 @@ class EntityTest extends TestCase
|
||||
|
||||
public function test_recently_created_pages_on_home()
|
||||
{
|
||||
$entityChain = $this->createEntityChainBelongingToUser($this->getNewUser());
|
||||
$entityChain = $this->createEntityChainBelongingToUser($this->getEditor());
|
||||
$this->asAdmin()->visit('/')
|
||||
->seeInElement('#recently-created-pages', $entityChain['page']->name);
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ class PageDraftTest extends TestCase
|
||||
->dontSeeInField('html', $addedContent);
|
||||
|
||||
$newContent = $this->page->html . $addedContent;
|
||||
$newUser = $this->getNewUser();
|
||||
$newUser = $this->getEditor();
|
||||
$this->pageRepo->saveUpdateDraft($this->page, ['html' => $newContent]);
|
||||
$this->actingAs($newUser)->visit($this->page->getUrl() . '/edit')
|
||||
->dontSeeInField('html', $newContent);
|
||||
@ -54,7 +54,7 @@ class PageDraftTest extends TestCase
|
||||
->dontSeeInField('html', $addedContent);
|
||||
|
||||
$newContent = $this->page->html . $addedContent;
|
||||
$newUser = $this->getNewUser();
|
||||
$newUser = $this->getEditor();
|
||||
$this->pageRepo->saveUpdateDraft($this->page, ['html' => $newContent]);
|
||||
|
||||
$this->actingAs($newUser)
|
||||
@ -79,7 +79,7 @@ class PageDraftTest extends TestCase
|
||||
{
|
||||
$book = \BookStack\Book::first();
|
||||
$chapter = $book->chapters->first();
|
||||
$newUser = $this->getNewUser();
|
||||
$newUser = $this->getEditor();
|
||||
|
||||
$this->actingAs($newUser)->visit('/')
|
||||
->visit($book->getUrl() . '/page/create')
|
||||
|
@ -9,7 +9,7 @@ class RestrictionsTest extends TestCase
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->user = $this->getNewUser();
|
||||
$this->user = $this->getEditor();
|
||||
$this->viewer = $this->getViewer();
|
||||
$this->restrictionService = $this->app[\BookStack\Services\PermissionService::class];
|
||||
}
|
||||
|
@ -14,7 +14,10 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
|
||||
* @var string
|
||||
*/
|
||||
protected $baseUrl = 'http://localhost';
|
||||
|
||||
// Local user instances
|
||||
private $admin;
|
||||
private $editor;
|
||||
|
||||
/**
|
||||
* Creates the application.
|
||||
@ -30,6 +33,10 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
|
||||
return $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current user context to be an admin.
|
||||
* @return $this
|
||||
*/
|
||||
public function asAdmin()
|
||||
{
|
||||
if($this->admin === null) {
|
||||
@ -39,6 +46,18 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
|
||||
return $this->actingAs($this->admin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current editor context to be an editor.
|
||||
* @return $this
|
||||
*/
|
||||
public function asEditor()
|
||||
{
|
||||
if($this->editor === null) {
|
||||
$this->editor = $this->getEditor();
|
||||
}
|
||||
return $this->actingAs($this->editor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Quickly sets an array of settings.
|
||||
* @param $settingsArray
|
||||
@ -79,7 +98,7 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getNewUser($attributes = [])
|
||||
protected function getEditor($attributes = [])
|
||||
{
|
||||
$user = factory(\BookStack\User::class)->create($attributes);
|
||||
$role = \BookStack\Role::getRole('editor');
|
||||
|
@ -33,7 +33,7 @@ class UserProfileTest extends TestCase
|
||||
|
||||
public function test_profile_page_shows_created_content_counts()
|
||||
{
|
||||
$newUser = $this->getNewUser();
|
||||
$newUser = $this->getEditor();
|
||||
|
||||
$this->asAdmin()->visit('/user/' . $newUser->id)
|
||||
->see($newUser->name)
|
||||
@ -52,7 +52,7 @@ class UserProfileTest extends TestCase
|
||||
|
||||
public function test_profile_page_shows_recent_activity()
|
||||
{
|
||||
$newUser = $this->getNewUser();
|
||||
$newUser = $this->getEditor();
|
||||
$this->actingAs($newUser);
|
||||
$entities = $this->createEntityChainBelongingToUser($newUser, $newUser);
|
||||
Activity::add($entities['book'], 'book_update', $entities['book']->id);
|
||||
@ -66,7 +66,7 @@ class UserProfileTest extends TestCase
|
||||
|
||||
public function test_clicking_user_name_in_activity_leads_to_profile_page()
|
||||
{
|
||||
$newUser = $this->getNewUser();
|
||||
$newUser = $this->getEditor();
|
||||
$this->actingAs($newUser);
|
||||
$entities = $this->createEntityChainBelongingToUser($newUser, $newUser);
|
||||
Activity::add($entities['book'], 'book_update', $entities['book']->id);
|
||||
|
Reference in New Issue
Block a user