Add visibility to component groups (#2027)

Implement visibility for the components groups. Closes #1892

Add functional test that asserts a guest can only see public items.

* Fix tests not running due to hitting the Setup page.

The missing `boostrap/cachet/testing.php` file is now generated the first time tests are ran.

* Add a functional test that asserts logged in users can see all items.

Add constants for possible values for the visible column/field of the ComponentGroup model.
Code review changes.

* Add API tests for component group visibility feature.

* Implement the visibility hidden option for a component group. Fixes #1892.

Add migration for the created_by column, in component_groups table.
Add methods to the ComponentGroup and User models to be able to work with the created_by column.
Hidden component groups are no longer displayed on the index page for loggedin users.
Add functional test for the dashboard page.
Save owner on create/edit component group.
Update the API tests for Component group visibility feature.

* Replace auth() usage with app(Guard::class).

* Apply StyleCI fixes.

* Drop the hidden visibility feature and fix all tests.

Some code review fixes too.

* Rename public to visible since it's a reserved keyword. Apply StyleCI fixes and correct typo.

* Code review changes.

* Tidy up component and component groups gathering.

* Code review changes and StyleCI fixes.

* Code review changes.

* Remove extra whitespace

* Remove useless method.
This commit is contained in:
Marius Palade
2016-10-02 15:57:32 +03:00
committed by James Brooks
parent bf769e1470
commit ad0954eb20
22 changed files with 601 additions and 26 deletions

View File

@@ -11,6 +11,9 @@
namespace CachetHQ\Tests\Cachet;
use CachetHQ\Cachet\Models\User;
use CachetHQ\Cachet\Settings\Cache;
use CachetHQ\Cachet\Settings\Repository;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\TestCase;
@@ -28,6 +31,13 @@ abstract class AbstractTestCase extends TestCase
*/
protected $baseUrl = 'http://localhost';
/**
* Test actor.
*
* @var User
*/
protected $user;
/**
* Creates the application.
*
@@ -41,4 +51,56 @@ abstract class AbstractTestCase extends TestCase
return $app;
}
/**
* Sign in an user if it's the case.
*
* @param User|null $user
*
* @return AbstractTestCase
*/
protected function signIn(User $user = null)
{
$this->user = $user ?: $this->createUser();
$this->be($this->user);
return $this;
}
/**
* Create and return a new user.
*
* @param array $properties
*
* @return User
*/
protected function createUser($properties = [])
{
return factory(User::class)->create($properties);
}
/**
* Set up the needed configuration to be able to run the tests.
*
* @return AbstractTestCase
*/
protected function setupConfig()
{
$env = $this->app->environment();
$repo = $this->app->make(Repository::class);
$cache = $this->app->make(Cache::class);
$loaded = $cache->load($env);
if ($loaded === false) {
$loaded = $repo->all();
$cache->store($env, $loaded);
}
$settings = array_merge($this->app->config->get('setting'), $loaded);
$this->app->config->set('setting', $settings);
return $this;
}
}

View File

@@ -11,6 +11,9 @@
namespace CachetHQ\Tests\Cachet\Api;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
/**
* This is the component group test class.
*
@@ -19,9 +22,13 @@ namespace CachetHQ\Tests\Cachet\Api;
*/
class ComponentGroupTest extends AbstractApiTestCase
{
const COMPONENT_GROUP_1_NAME = 'Component Group 1';
const COMPONENT_GROUP_2_NAME = 'Component Group 2';
public function testGetGroups()
{
$groups = factory('CachetHQ\Cachet\Models\ComponentGroup', 3)->create();
$groups = factory('CachetHQ\Cachet\Models\ComponentGroup', 3)
->create(['visible' => ComponentGroup::VISIBLE_GUEST]);
$this->get('/api/v1/components/groups');
$this->seeJson(['id' => $groups[0]->id]);
@@ -59,8 +66,9 @@ class ComponentGroupTest extends AbstractApiTestCase
'name' => 'Foo',
'order' => 1,
'collapsed' => 1,
'visible' => ComponentGroup::VISIBLE_GUEST,
]);
$this->seeJson(['name' => 'Foo', 'order' => 1, 'collapsed' => 1]);
$this->seeJson(['name' => 'Foo', 'order' => 1, 'collapsed' => 1, 'visible' => ComponentGroup::VISIBLE_GUEST]);
$this->assertResponseOk();
}
@@ -93,4 +101,58 @@ class ComponentGroupTest extends AbstractApiTestCase
$this->delete('/api/v1/components/groups/1');
$this->assertResponseStatus(204);
}
/** @test */
public function only_public_component_groups_are_shown_for_a_guest()
{
$this->createComponentGroups();
$this->get('/api/v1/components/groups')
->seeJson(['name' => self::COMPONENT_GROUP_1_NAME])
->dontSeeJson(['name' => self::COMPONENT_GROUP_2_NAME]);
$this->assertResponseOk();
}
/** @test */
public function all_component_groups_are_displayed_for_loggedin_users()
{
$this->createComponentGroups()
->signIn();
$this->get('/api/v1/components/groups')
->seeJson(['name' => self::COMPONENT_GROUP_1_NAME])
->seeJson(['name' => self::COMPONENT_GROUP_2_NAME]);
$this->assertResponseOk();
}
/**
* Set up the needed data for the tests.
*
* @return TestCase
*/
protected function createComponentGroups()
{
$this->createComponentGroup(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_GUEST)
->createComponentGroup(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_AUTHENTICATED);
return $this;
}
/**
* Create a component group.
* Also attaches a creator if any given as a parameter
* or exists in the test class.
*
* @param string $name
* @param string $visible
*
* @return AbstractApiTestCase
*/
protected function createComponentGroup($name, $visible)
{
factory(ComponentGroup::class)
->create(['name' => $name, 'visible' => $visible]);
return $this;
}
}

View File

@@ -14,6 +14,7 @@ namespace CachetHQ\Tests\Cachet\Bus\Commands\ComponentGroup;
use AltThree\TestBench\CommandTrait;
use CachetHQ\Cachet\Bus\Commands\ComponentGroup\AddComponentGroupCommand;
use CachetHQ\Cachet\Bus\Handlers\Commands\ComponentGroup\AddComponentGroupCommandHandler;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
@@ -28,9 +29,16 @@ class AddComponentGroupCommandTest extends AbstractTestCase
protected function getObjectAndParams()
{
$params = ['name' => 'Test', 'order' => 0, 'collapsed' => 1];
$params = [
'name' => 'Test',
'order' => 0,
'collapsed' => 1,
'visible' => ComponentGroup::VISIBLE_AUTHENTICATED,
];
$object = new AddComponentGroupCommand($params['name'], $params['order'], $params['collapsed']);
$object = new AddComponentGroupCommand(
$params['name'], $params['order'], $params['collapsed'], $params['visible']
);
return compact('params', 'object');
}

View File

@@ -29,12 +29,19 @@ class UpdateComponentGroupCommandTest extends AbstractTestCase
protected function getObjectAndParams()
{
$params = ['group' => new ComponentGroup(), 'name' => 'Foo', 'order' => 1, 'collapsed' => 2];
$params = [
'group' => new ComponentGroup(),
'name' => 'Foo',
'order' => 1,
'collapsed' => 2,
'visible' => ComponentGroup::VISIBLE_AUTHENTICATED,
];
$object = new UpdateComponentGroupCommand(
$params['group'],
$params['name'],
$params['order'],
$params['collapsed']
$params['collapsed'],
$params['visible']
);
return compact('params', 'object');

View File

@@ -0,0 +1,84 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Http\Controllers;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Setting;
use CachetHQ\Cachet\Models\User;
use CachetHQ\Tests\Cachet\AbstractTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\TestCase;
class DashboardControllerTest extends AbstractTestCase
{
use DatabaseMigrations;
const COMPONENT_GROUP_1_NAME = 'Component Group 1';
const COMPONENT_GROUP_2_NAME = 'Component Group 2';
/**
* @var User
*/
protected $user;
protected function setUp()
{
parent::setUp();
$this->setupPublicAndNonPublicComponentGroups()
->setupConfig();
}
/** @test */
public function on_dashboard_all_component_groups_are_displayed()
{
$this->signIn();
$this->visit('/dashboard')
->see(self::COMPONENT_GROUP_1_NAME)
->see(self::COMPONENT_GROUP_2_NAME);
}
/**
* Set up the needed data for the components groups tests.
*
* @return TestCase
*/
protected function setupPublicAndNonPublicComponentGroups()
{
$this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_GUEST)
->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_AUTHENTICATED);
factory(Setting::class)->create();
return $this;
}
/**
* Create a component group and add one component to it.
*
* @param string $name
* @param string $visible
*
* @return TestCase
*/
protected function createAComponentGroupAndAddAComponent($name, $visible)
{
factory(ComponentGroup::class)
->create(['name' => $name, 'visible' => $visible])
->components()
->save(factory(Component::class)->create());
return $this;
}
}

View File

@@ -0,0 +1,91 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Http\Controllers;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Setting;
use CachetHQ\Cachet\Models\User;
use CachetHQ\Tests\Cachet\AbstractTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class StatusPageControllerTest extends AbstractTestCase
{
use DatabaseMigrations;
const COMPONENT_GROUP_1_NAME = 'Component Group 1';
const COMPONENT_GROUP_2_NAME = 'Component Group 2';
/**
* @var User
*/
protected $user;
protected function setUp()
{
parent::setUp();
$this->setupPublicAndNonPublicComponentGroups()
->setupConfig();
}
/** @test */
public function on_index_only_public_component_groups_are_shown_to_a_guest()
{
$this->visit('/')
->see(self::COMPONENT_GROUP_1_NAME)
->dontSee(self::COMPONENT_GROUP_2_NAME);
}
/** @test */
public function on_index_all_component_groups_are_displayed_to_logged_in_users()
{
$this->signIn();
$this->visit('/')
->see(self::COMPONENT_GROUP_1_NAME)
->see(self::COMPONENT_GROUP_2_NAME);
}
/**
* Set up the needed data for the components groups tests.
*
* @return AbstractTestCase
*/
protected function setupPublicAndNonPublicComponentGroups()
{
$this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_GUEST)
->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_AUTHENTICATED);
factory(Setting::class)->create();
return $this;
}
/**
* Create a component group and add one component to it.
*
* @param string $name
* @param string $visible
*
* @return AbstractTestCase
*/
protected function createAComponentGroupAndAddAComponent($name, $visible)
{
factory(ComponentGroup::class)
->create(['name' => $name, 'visible' => $visible])
->components()
->save(factory(Component::class)->create());
return $this;
}
}