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
@@ -39,6 +39,13 @@ final class AddComponentGroupCommand
*/ */
public $collapsed; public $collapsed;
/**
* Is the component visible to public?
*
* @var int
*/
public $visible;
/** /**
* The validation rules. * The validation rules.
* *
@@ -48,6 +55,7 @@ final class AddComponentGroupCommand
'name' => 'required|string', 'name' => 'required|string',
'order' => 'int', 'order' => 'int',
'collapsed' => 'int|between:0,3', 'collapsed' => 'int|between:0,3',
'visible' => 'bool',
]; ];
/** /**
@@ -56,13 +64,15 @@ final class AddComponentGroupCommand
* @param string $name * @param string $name
* @param int $order * @param int $order
* @param int $collapsed * @param int $collapsed
* @param int $visible
* *
* @return void * @return void
*/ */
public function __construct($name, $order, $collapsed) public function __construct($name, $order, $collapsed, $visible)
{ {
$this->name = $name; $this->name = $name;
$this->order = (int) $order; $this->order = (int) $order;
$this->collapsed = $collapsed; $this->collapsed = $collapsed;
$this->visible = (int) $visible;
} }
} }
@@ -48,6 +48,13 @@ final class UpdateComponentGroupCommand
*/ */
public $collapsed; public $collapsed;
/**
* Is the component visible to public?
*
* @var int
*/
public $visible;
/** /**
* The validation rules. * The validation rules.
* *
@@ -57,6 +64,7 @@ final class UpdateComponentGroupCommand
'name' => 'string', 'name' => 'string',
'order' => 'int', 'order' => 'int',
'collapsed' => 'int|between:0,3', 'collapsed' => 'int|between:0,3',
'visible' => 'bool',
]; ];
/** /**
@@ -66,14 +74,16 @@ final class UpdateComponentGroupCommand
* @param string $name * @param string $name
* @param int $order * @param int $order
* @param int $collapsed * @param int $collapsed
* @param int $visible
* *
* @return void * @return void
*/ */
public function __construct(ComponentGroup $group, $name, $order, $collapsed) public function __construct(ComponentGroup $group, $name, $order, $collapsed, $visible)
{ {
$this->group = $group; $this->group = $group;
$this->name = $name; $this->name = $name;
$this->order = (int) $order; $this->order = (int) $order;
$this->collapsed = $collapsed; $this->collapsed = $collapsed;
$this->visible = (int) $visible;
} }
} }
@@ -30,6 +30,7 @@ class AddComponentGroupCommandHandler
'name' => $command->name, 'name' => $command->name,
'order' => $command->order, 'order' => $command->order,
'collapsed' => $command->collapsed, 'collapsed' => $command->collapsed,
'visible' => $command->visible,
]); ]);
event(new ComponentGroupWasAddedEvent($group)); event(new ComponentGroupWasAddedEvent($group));
@@ -46,6 +46,7 @@ class UpdateComponentGroupCommandHandler
'name' => $command->name, 'name' => $command->name,
'order' => $command->order, 'order' => $command->order,
'collapsed' => $command->collapsed, 'collapsed' => $command->collapsed,
'visible' => $command->visible,
]; ];
return array_filter($params, function ($val) { return array_filter($params, function ($val) {
+40 -4
View File
@@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Composers\Modules;
use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\ComponentGroup;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
/** /**
@@ -23,6 +24,25 @@ use Illuminate\Contracts\View\View;
*/ */
class ComponentsComposer class ComponentsComposer
{ {
/**
* The user session object.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $guard;
/**
* Creates a new components composer instance.
*
* @param \Illuminate\Contracts\Auth\Guard $guard
*
* @return void
*/
public function __construct(Guard $guard)
{
$this->guard = $guard;
}
/** /**
* Index page view composer. * Index page view composer.
* *
@@ -32,12 +52,28 @@ class ComponentsComposer
*/ */
public function compose(View $view) public function compose(View $view)
{ {
// Component & Component Group lists. $componentGroups = $this->getVisibleGroupedComponents();
$usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id'); $ungroupedComponents = Component::ungrouped()->get();
$componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get();
$ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get();
$view->withComponentGroups($componentGroups) $view->withComponentGroups($componentGroups)
->withUngroupedComponents($ungroupedComponents); ->withUngroupedComponents($ungroupedComponents);
} }
/**
* Get visible grouped components.
*
* @return \Illuminate\Support\Collection
*/
protected function getVisibleGroupedComponents()
{
$componentGroupsBuilder = ComponentGroup::query();
if (!$this->guard->check()) {
$componentGroupsBuilder->visible();
}
$usedComponentGroups = Component::grouped()->pluck('group_id');
return $componentGroupsBuilder->used($usedComponentGroups)
->get();
}
} }
@@ -106,10 +106,12 @@ class DemoSeederCommand extends Command
'name' => 'Websites', 'name' => 'Websites',
'order' => 1, 'order' => 1,
'collapsed' => 0, 'collapsed' => 0,
'visible' => ComponentGroup::VISIBLE_AUTHENTICATED,
], [ ], [
'name' => 'Alt Three', 'name' => 'Alt Three',
'order' => 2, 'order' => 2,
'collapsed' => 1, 'collapsed' => 1,
'visible' => ComponentGroup::VISIBLE_GUEST,
], ],
]; ];
@@ -16,6 +16,7 @@ use CachetHQ\Cachet\Bus\Commands\ComponentGroup\RemoveComponentGroupCommand;
use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand; use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand;
use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\ComponentGroup;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\QueryException; use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -29,6 +30,23 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
*/ */
class ComponentGroupController extends AbstractApiController class ComponentGroupController extends AbstractApiController
{ {
/**
* The user session object.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $guard;
/**
* Creates a new component group controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $guard
*/
public function __construct(Guard $guard)
{
$this->guard = $guard;
}
/** /**
* Get all groups. * Get all groups.
* *
@@ -37,6 +55,9 @@ class ComponentGroupController extends AbstractApiController
public function getGroups() public function getGroups()
{ {
$groups = ComponentGroup::query(); $groups = ComponentGroup::query();
if (!$this->guard->check()) {
$groups = ComponentGroup::visible();
}
$groups->search(Binput::except(['sort', 'order', 'per_page'])); $groups->search(Binput::except(['sort', 'order', 'per_page']));
@@ -74,7 +95,8 @@ class ComponentGroupController extends AbstractApiController
$group = dispatch(new AddComponentGroupCommand( $group = dispatch(new AddComponentGroupCommand(
Binput::get('name'), Binput::get('name'),
Binput::get('order', 0), Binput::get('order', 0),
Binput::get('collapsed', 0) Binput::get('collapsed', 0),
Binput::get('visible', ComponentGroup::VISIBLE_AUTHENTICATED)
)); ));
} catch (QueryException $e) { } catch (QueryException $e) {
throw new BadRequestHttpException(); throw new BadRequestHttpException();
@@ -97,7 +119,8 @@ class ComponentGroupController extends AbstractApiController
$group, $group,
Binput::get('name'), Binput::get('name'),
Binput::get('order'), Binput::get('order'),
Binput::get('collapsed') Binput::get('collapsed'),
Binput::get('visible')
)); ));
} catch (QueryException $e) { } catch (QueryException $e) {
throw new BadRequestHttpException(); throw new BadRequestHttpException();
@@ -277,7 +277,8 @@ class ComponentController extends Controller
$group = dispatch(new AddComponentGroupCommand( $group = dispatch(new AddComponentGroupCommand(
Binput::get('name'), Binput::get('name'),
Binput::get('order', 0), Binput::get('order', 0),
Binput::get('collapsed') Binput::get('collapsed'),
Binput::get('visible')
)); ));
} catch (ValidationException $e) { } catch (ValidationException $e) {
return Redirect::route('dashboard.components.groups.add') return Redirect::route('dashboard.components.groups.add')
@@ -304,7 +305,8 @@ class ComponentController extends Controller
$group, $group,
Binput::get('name'), Binput::get('name'),
$group->order, $group->order,
Binput::get('collapsed') Binput::get('collapsed'),
Binput::get('visible')
)); ));
} catch (ValidationException $e) { } catch (ValidationException $e) {
return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id]) return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id])
@@ -17,6 +17,7 @@ use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Subscriber; use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Routing\Controller; use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Config;
@@ -52,16 +53,25 @@ class DashboardController extends Controller
*/ */
protected $feed; protected $feed;
/**
* The user session object.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $guard;
/** /**
* Creates a new dashboard controller instance. * Creates a new dashboard controller instance.
* *
* @param \CachetHQ\Cachet\Integrations\Feed $feed * @param \CachetHQ\Cachet\Integrations\Feed $feed
* @param \Illuminate\Contracts\Auth\Guard $guard
* *
* @return void * @return void
*/ */
public function __construct(Feed $feed) public function __construct(Feed $feed, Guard $guard)
{ {
$this->feed = $feed; $this->feed = $feed;
$this->guard = $guard;
$this->startDate = new Date(); $this->startDate = new Date();
$this->dateTimeZone = Config::get('cachet.timezone'); $this->dateTimeZone = Config::get('cachet.timezone');
} }
@@ -86,9 +96,9 @@ class DashboardController extends Controller
$components = Component::orderBy('order')->get(); $components = Component::orderBy('order')->get();
$incidents = $this->getIncidents(); $incidents = $this->getIncidents();
$subscribers = $this->getSubscribers(); $subscribers = $this->getSubscribers();
$usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id');
$componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get(); $componentGroups = $this->getVisibleGroupedComponents();
$ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get(); $ungroupedComponents = Component::ungrouped()->get();
$welcomeUser = !Auth::user()->welcomed; $welcomeUser = !Auth::user()->welcomed;
if ($welcomeUser) { if ($welcomeUser) {
@@ -174,4 +184,22 @@ class DashboardController extends Controller
return $allSubscribers; return $allSubscribers;
} }
/**
* Get visible grouped components.
*
* @return \Illuminate\Support\Collection
*/
protected function getVisibleGroupedComponents()
{
$componentGroupsBuilder = ComponentGroup::query();
if (!$this->guard->check()) {
$componentGroupsBuilder = ComponentGroup::visible();
}
$usedComponentGroups = Component::grouped()->pluck('group_id');
return $componentGroupsBuilder->used($usedComponentGroups)
->get();
}
} }
+29
View File
@@ -188,6 +188,35 @@ class Component extends Model implements HasPresenter
return $query->where('enabled', false); return $query->where('enabled', false);
} }
/**
* Finds all ungrouped components.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUngrouped(Builder $query)
{
return $query->enabled()
->where('group_id', 0)
->orderBy('order')
->orderBy('created_at');
}
/**
* Finds all grouped components.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeGrouped(Builder $query)
{
return $query->enabled()
->where('group_id', '>', 0)
->groupBy('group_id');
}
/** /**
* Returns all of the tags on this component. * Returns all of the tags on this component.
* *
+48 -1
View File
@@ -15,13 +15,29 @@ use AltThree\Validator\ValidatingTrait;
use CachetHQ\Cachet\Models\Traits\SearchableTrait; use CachetHQ\Cachet\Models\Traits\SearchableTrait;
use CachetHQ\Cachet\Models\Traits\SortableTrait; use CachetHQ\Cachet\Models\Traits\SortableTrait;
use CachetHQ\Cachet\Presenters\ComponentGroupPresenter; use CachetHQ\Cachet\Presenters\ComponentGroupPresenter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use McCool\LaravelAutoPresenter\HasPresenter; use McCool\LaravelAutoPresenter\HasPresenter;
class ComponentGroup extends Model implements HasPresenter class ComponentGroup extends Model implements HasPresenter
{ {
use SearchableTrait, SortableTrait, ValidatingTrait; use SearchableTrait, SortableTrait, ValidatingTrait;
/**
* Viewable only authenticated users.
*
* @var int
*/
const VISIBLE_AUTHENTICATED = 0;
/**
* Viewable by public.
*
* @var int
*/
const VISIBLE_GUEST = 1;
/** /**
* The model's attributes. * The model's attributes.
* *
@@ -30,6 +46,7 @@ class ComponentGroup extends Model implements HasPresenter
protected $attributes = [ protected $attributes = [
'order' => 0, 'order' => 0,
'collapsed' => 0, 'collapsed' => 0,
'visible' => 0,
]; ];
/** /**
@@ -41,6 +58,7 @@ class ComponentGroup extends Model implements HasPresenter
'name' => 'string', 'name' => 'string',
'order' => 'int', 'order' => 'int',
'collapsed' => 'int', 'collapsed' => 'int',
'visible' => 'int',
]; ];
/** /**
@@ -48,7 +66,7 @@ class ComponentGroup extends Model implements HasPresenter
* *
* @var string[] * @var string[]
*/ */
protected $fillable = ['name', 'order', 'collapsed']; protected $fillable = ['name', 'order', 'collapsed', 'visible'];
/** /**
* The validation rules. * The validation rules.
@@ -59,6 +77,7 @@ class ComponentGroup extends Model implements HasPresenter
'name' => 'required|string', 'name' => 'required|string',
'order' => 'int', 'order' => 'int',
'collapsed' => 'int', 'collapsed' => 'int',
'visible' => 'bool',
]; ];
/** /**
@@ -71,6 +90,7 @@ class ComponentGroup extends Model implements HasPresenter
'name', 'name',
'order', 'order',
'collapsed', 'collapsed',
'visible',
]; ];
/** /**
@@ -83,6 +103,7 @@ class ComponentGroup extends Model implements HasPresenter
'name', 'name',
'order', 'order',
'collapsed', 'collapsed',
'visible',
]; ];
/** /**
@@ -141,4 +162,30 @@ class ComponentGroup extends Model implements HasPresenter
{ {
return ComponentGroupPresenter::class; return ComponentGroupPresenter::class;
} }
/**
* Finds all component groups which are visible to public.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeVisible(Builder $query)
{
return $query->where('visible', self::VISIBLE_GUEST);
}
/**
* Finds all used component groups.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Support\Collection $usedComponentGroups
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUsed(Builder $query, Collection $usedComponentGroups)
{
return $query->whereIn('id', $usedComponentGroups)
->orderBy('order');
}
} }
+9
View File
@@ -15,6 +15,7 @@ use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\IncidentTemplate; use CachetHQ\Cachet\Models\IncidentTemplate;
use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\MetricPoint; use CachetHQ\Cachet\Models\MetricPoint;
use CachetHQ\Cachet\Models\Setting;
use CachetHQ\Cachet\Models\Subscriber; use CachetHQ\Cachet\Models\Subscriber;
use CachetHQ\Cachet\Models\Subscription; use CachetHQ\Cachet\Models\Subscription;
use CachetHQ\Cachet\Models\User; use CachetHQ\Cachet\Models\User;
@@ -35,6 +36,7 @@ $factory->define(ComponentGroup::class, function ($faker) {
'name' => $faker->words(2, true), 'name' => $faker->words(2, true),
'order' => 0, 'order' => 0,
'collapsed' => random_int(0, 3), 'collapsed' => random_int(0, 3),
'visible' => $faker->boolean(),
]; ];
}); });
@@ -77,6 +79,13 @@ $factory->define(MetricPoint::class, function ($faker) {
]; ];
}); });
$factory->define(Setting::class, function ($faker) {
return [
'name' => 'app_name',
'value' => 'Cachet Test Demo',
];
});
$factory->define(Subscriber::class, function ($faker) { $factory->define(Subscriber::class, function ($faker) {
return [ return [
'email' => $faker->safeEmail, 'email' => $faker->safeEmail,
@@ -0,0 +1,46 @@
<?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.
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AlterTableComponentGroupsAddVisibleColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('component_groups', function (Blueprint $table) {
$table->tinyInteger('visible')
->after('order')
->unsigned()
->default(\CachetHQ\Cachet\Models\ComponentGroup::VISIBLE_AUTHENTICATED);
$table->index('visible');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('component_groups', function (Blueprint $table) {
$table->dropColumn('visible');
});
}
}
+4 -1
View File
@@ -78,10 +78,13 @@ return [
'groups' => [ 'groups' => [
'name' => 'Name', 'name' => 'Name',
'collapsing' => 'Choose visibility of the group', 'collapsing' => 'Expand/Collapse options',
'visible' => 'Always expanded', 'visible' => 'Always expanded',
'collapsed' => 'Collapse the group by default', 'collapsed' => 'Collapse the group by default',
'collapsed_incident' => 'Collapse the group, but expand if there are issues', 'collapsed_incident' => 'Collapse the group, but expand if there are issues',
'visibility' => 'Visibility',
'visibility_public' => 'Visible to public',
'visibility_authenticated' => 'Visible only to logged in users',
], ],
], ],
@@ -29,6 +29,13 @@
<option value="2">{{ trans('forms.components.groups.collapsed_incident') }}</option> <option value="2">{{ trans('forms.components.groups.collapsed_incident') }}</option>
</select> </select>
</div> </div>
<div class="form-group">
<label>{{ trans('forms.components.groups.visibility') }}</label>
<select name="collapsed" class="form-control" required>
<option value="0">{{ trans('forms.components.groups.visibility_authenticated') }}</option>
<option value="1">{{ trans('forms.components.groups.visibility_public') }}</option>
</select>
</div>
</fieldset> </fieldset>
<div class="btn-group"> <div class="btn-group">
@@ -29,6 +29,13 @@
<option value="2" {{ $group->collapsed === 2 ? "selected" : null }}>{{ trans('forms.components.groups.collapsed_incident') }}</option> <option value="2" {{ $group->collapsed === 2 ? "selected" : null }}>{{ trans('forms.components.groups.collapsed_incident') }}</option>
</select> </select>
</div> </div>
<div class="form-group">
<label>{{ trans('forms.components.groups.visibility') }}</label>
<select name="visible" class="form-control" required>
<option value="0" {{ $group->visible === 0 ? "selected" : null }}>{{ trans('forms.components.groups.visibility_authenticated') }}</option>
<option value="1" {{ $group->visible === 1 ? "selected" : null }}>{{ trans('forms.components.groups.visibility_public') }}</option>
</select>
</div>
</fieldset> </fieldset>
<div class="btn-group"> <div class="btn-group">
+62
View File
@@ -11,6 +11,9 @@
namespace CachetHQ\Tests\Cachet; 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\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\TestCase; use Illuminate\Foundation\Testing\TestCase;
@@ -28,6 +31,13 @@ abstract class AbstractTestCase extends TestCase
*/ */
protected $baseUrl = 'http://localhost'; protected $baseUrl = 'http://localhost';
/**
* Test actor.
*
* @var User
*/
protected $user;
/** /**
* Creates the application. * Creates the application.
* *
@@ -41,4 +51,56 @@ abstract class AbstractTestCase extends TestCase
return $app; 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;
}
} }
+64 -2
View File
@@ -11,6 +11,9 @@
namespace CachetHQ\Tests\Cachet\Api; namespace CachetHQ\Tests\Cachet\Api;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
/** /**
* This is the component group test class. * This is the component group test class.
* *
@@ -19,9 +22,13 @@ namespace CachetHQ\Tests\Cachet\Api;
*/ */
class ComponentGroupTest extends AbstractApiTestCase class ComponentGroupTest extends AbstractApiTestCase
{ {
const COMPONENT_GROUP_1_NAME = 'Component Group 1';
const COMPONENT_GROUP_2_NAME = 'Component Group 2';
public function testGetGroups() 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->get('/api/v1/components/groups');
$this->seeJson(['id' => $groups[0]->id]); $this->seeJson(['id' => $groups[0]->id]);
@@ -59,8 +66,9 @@ class ComponentGroupTest extends AbstractApiTestCase
'name' => 'Foo', 'name' => 'Foo',
'order' => 1, 'order' => 1,
'collapsed' => 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(); $this->assertResponseOk();
} }
@@ -93,4 +101,58 @@ class ComponentGroupTest extends AbstractApiTestCase
$this->delete('/api/v1/components/groups/1'); $this->delete('/api/v1/components/groups/1');
$this->assertResponseStatus(204); $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;
}
} }
@@ -14,6 +14,7 @@ namespace CachetHQ\Tests\Cachet\Bus\Commands\ComponentGroup;
use AltThree\TestBench\CommandTrait; use AltThree\TestBench\CommandTrait;
use CachetHQ\Cachet\Bus\Commands\ComponentGroup\AddComponentGroupCommand; use CachetHQ\Cachet\Bus\Commands\ComponentGroup\AddComponentGroupCommand;
use CachetHQ\Cachet\Bus\Handlers\Commands\ComponentGroup\AddComponentGroupCommandHandler; use CachetHQ\Cachet\Bus\Handlers\Commands\ComponentGroup\AddComponentGroupCommandHandler;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Tests\Cachet\AbstractTestCase; use CachetHQ\Tests\Cachet\AbstractTestCase;
/** /**
@@ -28,9 +29,16 @@ class AddComponentGroupCommandTest extends AbstractTestCase
protected function getObjectAndParams() 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'); return compact('params', 'object');
} }
@@ -29,12 +29,19 @@ class UpdateComponentGroupCommandTest extends AbstractTestCase
protected function getObjectAndParams() 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( $object = new UpdateComponentGroupCommand(
$params['group'], $params['group'],
$params['name'], $params['name'],
$params['order'], $params['order'],
$params['collapsed'] $params['collapsed'],
$params['visible']
); );
return compact('params', 'object'); return compact('params', 'object');
@@ -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;
}
}
@@ -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;
}
}