From 21e0ac702b966936dc06b0f80246fb7d7316cfe0 Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Sun, 31 Jul 2016 11:36:21 +0300 Subject: [PATCH 01/15] Implement visibility for the components groups. Part of #1897. Add functional test that asserts a guest can only see public items. --- .../AddComponentGroupCommand.php | 12 ++- .../UpdateComponentGroupCommand.php | 12 ++- .../AddComponentGroupCommandHandler.php | 1 + .../UpdateComponentGroupCommandHandler.php | 1 + app/Composers/Modules/ComponentsComposer.php | 7 +- .../Api/ComponentGroupController.php | 6 +- .../Dashboard/ComponentController.php | 6 +- app/Models/ComponentGroup.php | 38 ++++++- database/factories/ModelFactory.php | 23 ++-- ...erTableComponentGroupsAddVisibleColumn.php | 34 ++++++ resources/lang/en/forms.php | 14 ++- .../dashboard/components/groups/add.blade.php | 9 ++ .../components/groups/edit.blade.php | 8 ++ .../AddComponentGroupCommandTest.php | 6 +- .../UpdateComponentGroupCommandTest.php | 5 +- .../Controllers/StatusPageControllerTest.php | 102 ++++++++++++++++++ 16 files changed, 257 insertions(+), 27 deletions(-) create mode 100644 database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php create mode 100644 tests/Http/Controllers/StatusPageControllerTest.php diff --git a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php index e68f24fd09d8..7aeee3adec3c 100644 --- a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php @@ -39,6 +39,13 @@ final class AddComponentGroupCommand */ public $collapsed; + /** + * To whom should the component group be visible? + * + * @var int + */ + public $visible; + /** * The validation rules. * @@ -48,6 +55,7 @@ final class AddComponentGroupCommand 'name' => 'required|string', 'order' => 'int', 'collapsed' => 'int|between:0,3', + 'visible' => 'int|between:0,3', ]; /** @@ -56,13 +64,15 @@ final class AddComponentGroupCommand * @param string $name * @param int $order * @param int $collapsed + * @param int $visible * * @return void */ - public function __construct($name, $order, $collapsed) + public function __construct($name, $order, $collapsed, $visible) { $this->name = $name; $this->order = (int) $order; $this->collapsed = $collapsed; + $this->visible = (int) $visible; } } diff --git a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php index a20c49448557..572159c86b73 100644 --- a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php @@ -48,6 +48,13 @@ final class UpdateComponentGroupCommand */ public $collapsed; + /** + * To whom should the component group be visible? + * + * @var int + */ + public $visible; + /** * The validation rules. * @@ -57,6 +64,7 @@ final class UpdateComponentGroupCommand 'name' => 'string', 'order' => 'int', 'collapsed' => 'int|between:0,3', + 'visible' => 'int|between:0,3', ]; /** @@ -66,14 +74,16 @@ final class UpdateComponentGroupCommand * @param string $name * @param int $order * @param int $collapsed + * @param int $visible * * @return void */ - public function __construct(ComponentGroup $group, $name, $order, $collapsed) + public function __construct(ComponentGroup $group, $name, $order, $collapsed, $visible) { $this->group = $group; $this->name = $name; $this->order = (int) $order; $this->collapsed = $collapsed; + $this->visible = (int) $visible; } } diff --git a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php index 9066df512436..b2a935a86e97 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php @@ -30,6 +30,7 @@ public function handle(AddComponentGroupCommand $command) 'name' => $command->name, 'order' => $command->order, 'collapsed' => $command->collapsed, + 'visible' => $command->visible, ]); event(new ComponentGroupWasAddedEvent($group)); diff --git a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php index 4ea401869f49..ef0f3a07ef88 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php @@ -46,6 +46,7 @@ protected function filter(UpdateComponentGroupCommand $command) 'name' => $command->name, 'order' => $command->order, 'collapsed' => $command->collapsed, + 'visible' => $command->visible, ]; return array_filter($params, function ($val) { diff --git a/app/Composers/Modules/ComponentsComposer.php b/app/Composers/Modules/ComponentsComposer.php index a5df06ba5c11..28b4486e753b 100644 --- a/app/Composers/Modules/ComponentsComposer.php +++ b/app/Composers/Modules/ComponentsComposer.php @@ -34,7 +34,12 @@ public function compose(View $view) { // Component & Component Group lists. $usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id'); - $componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get(); + + $componentGroupsBuilder = ComponentGroup::guest(); + if (auth()->check()) { + $componentGroupsBuilder = ComponentGroup::loggedIn(); + } + $componentGroups = $componentGroupsBuilder->whereIn('id', $usedComponentGroups)->orderBy('order')->get(); $ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get(); $view->withComponentGroups($componentGroups) diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index 9c424b798233..de17c87ff527 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -74,7 +74,8 @@ public function postGroups() $group = dispatch(new AddComponentGroupCommand( Binput::get('name'), Binput::get('order', 0), - Binput::get('collapsed', 0) + Binput::get('collapsed', 0), + Binput::get('visible', 2) )); } catch (QueryException $e) { throw new BadRequestHttpException(); @@ -97,7 +98,8 @@ public function putGroup(ComponentGroup $group) $group, Binput::get('name'), Binput::get('order'), - Binput::get('collapsed') + Binput::get('collapsed'), + Binput::get('visible') )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 38a021f453cd..6f0f0f2a5d6e 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -277,7 +277,8 @@ public function postAddComponentGroup() $group = dispatch(new AddComponentGroupCommand( Binput::get('name'), Binput::get('order', 0), - Binput::get('collapsed') + Binput::get('collapsed'), + Binput::get('visible') )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.add') @@ -304,7 +305,8 @@ public function updateComponentGroupAction(ComponentGroup $group) $group, Binput::get('name'), $group->order, - Binput::get('collapsed') + Binput::get('collapsed'), + Binput::get('visible') )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id]) diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index 345f22101be5..0d439fc46445 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -12,11 +12,12 @@ namespace CachetHQ\Cachet\Models; use AltThree\Validator\ValidatingTrait; -use CachetHQ\Cachet\Models\Traits\SearchableTrait; -use CachetHQ\Cachet\Models\Traits\SortableTrait; -use CachetHQ\Cachet\Presenters\ComponentGroupPresenter; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; use McCool\LaravelAutoPresenter\HasPresenter; +use CachetHQ\Cachet\Models\Traits\SortableTrait; +use CachetHQ\Cachet\Models\Traits\SearchableTrait; +use CachetHQ\Cachet\Presenters\ComponentGroupPresenter; class ComponentGroup extends Model implements HasPresenter { @@ -30,6 +31,7 @@ class ComponentGroup extends Model implements HasPresenter protected $attributes = [ 'order' => 0, 'collapsed' => 0, + 'visible' => 0, ]; /** @@ -41,6 +43,7 @@ class ComponentGroup extends Model implements HasPresenter 'name' => 'string', 'order' => 'int', 'collapsed' => 'int', + 'visible' => 'int', ]; /** @@ -48,7 +51,7 @@ class ComponentGroup extends Model implements HasPresenter * * @var string[] */ - protected $fillable = ['name', 'order', 'collapsed']; + protected $fillable = ['name', 'order', 'collapsed', 'visible']; /** * The validation rules. @@ -59,6 +62,7 @@ class ComponentGroup extends Model implements HasPresenter 'name' => 'required|string', 'order' => 'int', 'collapsed' => 'int', + 'visible' => 'int', ]; /** @@ -71,6 +75,7 @@ class ComponentGroup extends Model implements HasPresenter 'name', 'order', 'collapsed', + 'visible', ]; /** @@ -83,6 +88,7 @@ class ComponentGroup extends Model implements HasPresenter 'name', 'order', 'collapsed', + 'visible', ]; /** @@ -141,4 +147,28 @@ public function getPresenterClass() { 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 scopeGuest(Builder $query) + { + return $query->where('visible', 0); + } + + /** + * Finds all component groups which are only visible to logged in users. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeLoggedIn(Builder $query) + { + return $query->where('visible', '>=', 1); + } } diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 3c70bdbeb169..76b3f4bbfb9d 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -9,16 +9,17 @@ * file that was distributed with this source code. */ -use CachetHQ\Cachet\Models\Component; -use CachetHQ\Cachet\Models\ComponentGroup; -use CachetHQ\Cachet\Models\Incident; -use CachetHQ\Cachet\Models\IncidentTemplate; +use Carbon\Carbon; +use CachetHQ\Cachet\Models\User; use CachetHQ\Cachet\Models\Metric; -use CachetHQ\Cachet\Models\MetricPoint; +use CachetHQ\Cachet\Models\Setting; +use CachetHQ\Cachet\Models\Incident; +use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Subscriber; +use CachetHQ\Cachet\Models\MetricPoint; use CachetHQ\Cachet\Models\Subscription; -use CachetHQ\Cachet\Models\User; -use Carbon\Carbon; +use CachetHQ\Cachet\Models\ComponentGroup; +use CachetHQ\Cachet\Models\IncidentTemplate; $factory->define(Component::class, function ($faker) { return [ @@ -35,6 +36,7 @@ 'name' => $faker->words(2, true), 'order' => 0, 'collapsed' => random_int(0, 3), + 'visible' => random_int(0, 2), ]; }); @@ -77,6 +79,13 @@ ]; }); +$factory->define(Setting::class, function ($faker) { + return [ + 'name' => 'app.name', + 'value' => 'Cachet Test Demo', + ]; +}); + $factory->define(Subscriber::class, function ($faker) { return [ 'email' => $faker->safeEmail, diff --git a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php new file mode 100644 index 000000000000..262fe9f224b8 --- /dev/null +++ b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php @@ -0,0 +1,34 @@ +integer('visible')->after('order')->unsigned()->default(2); + + $table->index('visible'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('component_groups', function (Blueprint $table) { + $table->dropColumn('visible'); + }); + } +} diff --git a/resources/lang/en/forms.php b/resources/lang/en/forms.php index fac80593fe56..30e99dd23a71 100644 --- a/resources/lang/en/forms.php +++ b/resources/lang/en/forms.php @@ -77,11 +77,15 @@ 'enabled' => 'Component enabled?', 'groups' => [ - 'name' => 'Name', - 'collapsing' => 'Choose visibility of the group', - 'visible' => 'Always expanded', - 'collapsed' => 'Collapse the group by default', - 'collapsed_incident' => 'Collapse the group, but expand if there are issues', + 'name' => 'Name', + 'collapsing' => 'Expand/Collapse options', + 'visible' => 'Always expanded', + 'collapsed' => 'Collapse the group by default', + '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', + 'visibility_hidden' => 'Hidden', ], ], diff --git a/resources/views/dashboard/components/groups/add.blade.php b/resources/views/dashboard/components/groups/add.blade.php index dc843f9d657e..2295e213c3fd 100644 --- a/resources/views/dashboard/components/groups/add.blade.php +++ b/resources/views/dashboard/components/groups/add.blade.php @@ -29,6 +29,15 @@ +
+ + +
+
diff --git a/resources/views/dashboard/components/groups/edit.blade.php b/resources/views/dashboard/components/groups/edit.blade.php index 39f0519e7384..833e38a3c5a5 100644 --- a/resources/views/dashboard/components/groups/edit.blade.php +++ b/resources/views/dashboard/components/groups/edit.blade.php @@ -29,6 +29,14 @@
+
+ + +
diff --git a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php index 8f4253fe81e1..87c178bed59f 100644 --- a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php @@ -28,9 +28,11 @@ class AddComponentGroupCommandTest extends AbstractTestCase protected function getObjectAndParams() { - $params = ['name' => 'Test', 'order' => 0, 'collapsed' => 1]; + $params = ['name' => 'Test', 'order' => 0, 'collapsed' => 1, 'visible' => 2]; - $object = new AddComponentGroupCommand($params['name'], $params['order'], $params['collapsed']); + $object = new AddComponentGroupCommand( + $params['name'], $params['order'], $params['collapsed'], $params['visible'] + ); return compact('params', 'object'); } diff --git a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php index 6db036bd3bd9..4a4ff50e3ea6 100644 --- a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php @@ -29,12 +29,13 @@ 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' => 1]; $object = new UpdateComponentGroupCommand( $params['group'], $params['name'], $params['order'], - $params['collapsed'] + $params['collapsed'], + $params['visible'] ); return compact('params', 'object'); diff --git a/tests/Http/Controllers/StatusPageControllerTest.php b/tests/Http/Controllers/StatusPageControllerTest.php new file mode 100644 index 000000000000..4658f4b6a019 --- /dev/null +++ b/tests/Http/Controllers/StatusPageControllerTest.php @@ -0,0 +1,102 @@ +setupData(); + } + + /** @test */ + public function only_public_component_groups_are_shown_for_a_guest() + { + $this->visit('/') + ->see(self::COMPONENT_GROUP_1_NAME) + ->dontSee(self::COMPONENT_GROUP_2_NAME) + ->dontSee(self::COMPONENT_GROUP_3_NAME); + } + + /** + * Set up the needed data for the tests. + * + * @return TestCase + */ + protected function setupData() + { + $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, 0); + $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, 1); + $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, 2); + + return $this; + } + + /** + * Create a component group and add one component to it. + * + * @param string $name + * @param string $visible + */ + protected function createAComponentGroupAndAddAComponent($name, $visible) + { + factory(ComponentGroup::class) + ->create(['name' => $name, 'visible' => $visible]) + ->components() + ->save(factory(Component::class)->create()); + } + + /** + * Sign in an user if it's the case. + * + * @param User|null $user + * + * @return TestCase + */ + protected function signIn($user = null) + { + if (isset($user)) { + $this->user = $user; + } + + if (is_null($this->user)) { + $this->createUser(); + } + + $this->actingAs($this->user); + + return $this; + } + + /** + * Create an user and assign it to the user property of the class. + * + * @return TestCase + */ + protected function createUser() + { + $this->user = factory(User::class)->create(); + + return $this; + } +} From 11b75e1de7bc728ac306bdf455b7ad23fc196fbb Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Thu, 4 Aug 2016 22:47:20 +0300 Subject: [PATCH 02/15] 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. --- database/factories/ModelFactory.php | 2 +- .../Controllers/StatusPageControllerTest.php | 36 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 76b3f4bbfb9d..cdf54d47e40d 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -81,7 +81,7 @@ $factory->define(Setting::class, function ($faker) { return [ - 'name' => 'app.name', + 'name' => 'app_name', 'value' => 'Cachet Test Demo', ]; }); diff --git a/tests/Http/Controllers/StatusPageControllerTest.php b/tests/Http/Controllers/StatusPageControllerTest.php index 4658f4b6a019..eafd7a91e5ca 100644 --- a/tests/Http/Controllers/StatusPageControllerTest.php +++ b/tests/Http/Controllers/StatusPageControllerTest.php @@ -4,7 +4,10 @@ use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; +use CachetHQ\Cachet\Models\Setting; use CachetHQ\Cachet\Models\User; +use CachetHQ\Cachet\Settings\Cache; +use CachetHQ\Cachet\Settings\Repository; use CachetHQ\Tests\Cachet\AbstractTestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\TestCase; @@ -26,7 +29,8 @@ protected function setUp() { parent::setUp(); - $this->setupData(); + $this->setupData() + ->setupConfig(); } /** @test */ @@ -49,6 +53,8 @@ protected function setupData() $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, 1); $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, 2); + factory(Setting::class)->create(); + return $this; } @@ -99,4 +105,32 @@ protected function createUser() return $this; } + + /** + * Set up the needed configuration to be able to run the tests. + * + * @return TestCase + */ + protected function setupConfig() + { + $env = $this->app->environment(); + $repo = $this->app->make(Repository::class); + $cache = $this->app->make(Cache::class); + $loaded = $cache->load($env); + + try { + 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); + } catch (Exception $e) { + // + } + + return $this; + } } From 11a3a70e272e252d40b75bc5949e5dff36b189d9 Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Thu, 4 Aug 2016 23:05:04 +0300 Subject: [PATCH 03/15] 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. --- app/Models/ComponentGroup.php | 19 +++++++++++++++++-- .../Controllers/StatusPageControllerTest.php | 18 +++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index 0d439fc46445..746d168c0bd2 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -23,6 +23,21 @@ class ComponentGroup extends Model implements HasPresenter { use SearchableTrait, SortableTrait, ValidatingTrait; + /** + * Viewable by public. + */ + const VISIBLE_PUBLIC = 0; + + /** + * Viewable by logged in users. + */ + const VISIBLE_LOGGED_IN = 1; + + /** + * Hidden. + */ + const VISIBLE_HIDDEN = 2; + /** * The model's attributes. * @@ -157,7 +172,7 @@ public function getPresenterClass() */ public function scopeGuest(Builder $query) { - return $query->where('visible', 0); + return $query->where('visible', self::VISIBLE_PUBLIC); } /** @@ -169,6 +184,6 @@ public function scopeGuest(Builder $query) */ public function scopeLoggedIn(Builder $query) { - return $query->where('visible', '>=', 1); + return $query->where('visible', '>=', self::VISIBLE_PUBLIC); } } diff --git a/tests/Http/Controllers/StatusPageControllerTest.php b/tests/Http/Controllers/StatusPageControllerTest.php index eafd7a91e5ca..c1851d0df861 100644 --- a/tests/Http/Controllers/StatusPageControllerTest.php +++ b/tests/Http/Controllers/StatusPageControllerTest.php @@ -2,6 +2,7 @@ namespace CachetHQ\Tests\Cachet\Http\Controllers; +use Exception; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\Setting; @@ -42,6 +43,17 @@ public function only_public_component_groups_are_shown_for_a_guest() ->dontSee(self::COMPONENT_GROUP_3_NAME); } + /** @test */ + public function all_component_groups_are_displayed_for_loggedin_users() + { + $this->signIn(); + + $this->visit('/') + ->see(self::COMPONENT_GROUP_1_NAME) + ->see(self::COMPONENT_GROUP_2_NAME) + ->see(self::COMPONENT_GROUP_3_NAME); + } + /** * Set up the needed data for the tests. * @@ -49,9 +61,9 @@ public function only_public_component_groups_are_shown_for_a_guest() */ protected function setupData() { - $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, 0); - $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, 1); - $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, 2); + $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC); + $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN); + $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN); factory(Setting::class)->create(); From 8588806d5a3b30f366d1a4df7f801f4ed12e9cca Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Sat, 6 Aug 2016 11:58:27 +0300 Subject: [PATCH 04/15] Add API tests for component group visibility feature. --- .../Api/ComponentGroupController.php | 6 +- tests/Api/ComponentGroupTest.php | 61 ++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index de17c87ff527..b43d386fc940 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -36,7 +36,11 @@ class ComponentGroupController extends AbstractApiController */ public function getGroups() { - $groups = ComponentGroup::query(); + $groups = ComponentGroup::query()->guest(); + + if (auth()->check()) { + $groups = ComponentGroup::query()->loggedIn(); + } $groups->search(Binput::except(['sort', 'order', 'per_page'])); diff --git a/tests/Api/ComponentGroupTest.php b/tests/Api/ComponentGroupTest.php index 1da912a9a8ef..994ca45efdaf 100644 --- a/tests/Api/ComponentGroupTest.php +++ b/tests/Api/ComponentGroupTest.php @@ -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,14 @@ */ class ComponentGroupTest extends AbstractApiTestCase { + const COMPONENT_GROUP_1_NAME = 'Component Group 1'; + const COMPONENT_GROUP_2_NAME = 'Component Group 2'; + const COMPONENT_GROUP_3_NAME = 'Component Group 3'; + public function testGetGroups() { - $groups = factory('CachetHQ\Cachet\Models\ComponentGroup', 3)->create(); + $groups = factory('CachetHQ\Cachet\Models\ComponentGroup', 3) + ->create(['visible' => ComponentGroup::VISIBLE_PUBLIC]); $this->get('/api/v1/components/groups'); $this->seeJson(['id' => $groups[0]->id]); @@ -93,4 +101,55 @@ public function testDeleteGroup() $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]) + ->dontSeeJson(['name' => self::COMPONENT_GROUP_3_NAME]); + $this->assertResponseOk(); + } + + /** @test */ + public function all_component_groups_are_displayed_for_loggedin_users() + { + $this->createComponentGroups() + ->beUser(); + + $this->get('/api/v1/components/groups') + ->seeJson(['name' => self::COMPONENT_GROUP_1_NAME]) + ->seeJson(['name' => self::COMPONENT_GROUP_2_NAME]) + ->seeJson(['name' => self::COMPONENT_GROUP_3_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_PUBLIC); + $this->createComponentGroup(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN); + $this->createComponentGroup(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN); + + return $this; + } + + /** + * Create a component group. + * + * @param string $name + * @param string $visible + */ + protected function createComponentGroup($name, $visible) + { + factory(ComponentGroup::class) + ->create(['name' => $name, 'visible' => $visible]); + } } From 24c61ed68393313742dfd7e5132e12abe1ad8c77 Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Sat, 6 Aug 2016 13:17:40 +0300 Subject: [PATCH 05/15] 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. --- .../AddComponentGroupCommand.php | 18 ++- .../UpdateComponentGroupCommand.php | 20 ++- .../AddComponentGroupCommandHandler.php | 1 + .../UpdateComponentGroupCommandHandler.php | 1 + app/Composers/Modules/ComponentsComposer.php | 20 ++- .../Api/ComponentGroupController.php | 21 +-- .../Dashboard/ComponentController.php | 6 +- .../Dashboard/DashboardController.php | 20 ++- app/Models/ComponentGroup.php | 60 +++++++-- app/Models/User.php | 21 ++- database/factories/ModelFactory.php | 9 +- ...TableComponentGroupsAddCreatedByColumn.php | 34 +++++ tests/AbstractTestCase.php | 82 ++++++++++-- tests/Api/ComponentGroupTest.php | 51 ++++++- .../AddComponentGroupCommandTest.php | 4 +- .../UpdateComponentGroupCommandTest.php | 12 +- .../Dashboard/DashboardControllerTest.php | 102 ++++++++++++++ .../Controllers/StatusPageControllerTest.php | 124 +++++++----------- 18 files changed, 456 insertions(+), 150 deletions(-) create mode 100644 database/migrations/2016_08_06_100357_AlterTableComponentGroupsAddCreatedByColumn.php create mode 100644 tests/Http/Controllers/Dashboard/DashboardControllerTest.php diff --git a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php index 7aeee3adec3c..81212cfb0aa0 100644 --- a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php @@ -46,16 +46,22 @@ final class AddComponentGroupCommand */ public $visible; + /** + * The id of the creator of the component group. + */ + public $created_by; + /** * The validation rules. * * @var string[] */ public $rules = [ - 'name' => 'required|string', - 'order' => 'int', - 'collapsed' => 'int|between:0,3', - 'visible' => 'int|between:0,3', + 'name' => 'required|string', + 'order' => 'int', + 'collapsed' => 'int|between:0,3', + 'visible' => 'int|between:0,3', + 'created_by' => 'int', ]; /** @@ -65,14 +71,16 @@ final class AddComponentGroupCommand * @param int $order * @param int $collapsed * @param int $visible + * @param int $created_by * * @return void */ - public function __construct($name, $order, $collapsed, $visible) + public function __construct($name, $order, $collapsed, $visible, $created_by) { $this->name = $name; $this->order = (int) $order; $this->collapsed = $collapsed; $this->visible = (int) $visible; + $this->created_by = (int) $created_by; } } diff --git a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php index 572159c86b73..b92a9d155466 100644 --- a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php @@ -55,6 +55,11 @@ final class UpdateComponentGroupCommand */ public $visible; + /** + * The id of the creator of the component group. + */ + public $created_by; + /** * The validation rules. * @@ -65,25 +70,28 @@ final class UpdateComponentGroupCommand 'order' => 'int', 'collapsed' => 'int|between:0,3', 'visible' => 'int|between:0,3', + 'created_by' => 'int', ]; /** * Create a add component group command instance. * - * @param \CachetHQ\Cachet\Models\ComponentGroup $group - * @param string $name - * @param int $order - * @param int $collapsed - * @param int $visible + * @param ComponentGroup $group + * @param string $name + * @param int $order + * @param int $collapsed + * @param into $visible + * @param int $created_by * * @return void */ - public function __construct(ComponentGroup $group, $name, $order, $collapsed, $visible) + public function __construct(ComponentGroup $group, $name, $order, $collapsed, $visible, $created_by) { $this->group = $group; $this->name = $name; $this->order = (int) $order; $this->collapsed = $collapsed; $this->visible = (int) $visible; + $this->created_by = (int) $created_by; } } diff --git a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php index b2a935a86e97..a5312d401996 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php @@ -31,6 +31,7 @@ public function handle(AddComponentGroupCommand $command) 'order' => $command->order, 'collapsed' => $command->collapsed, 'visible' => $command->visible, + 'created_by' => $command->created_by, ]); event(new ComponentGroupWasAddedEvent($group)); diff --git a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php index ef0f3a07ef88..96a79223e994 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php @@ -47,6 +47,7 @@ protected function filter(UpdateComponentGroupCommand $command) 'order' => $command->order, 'collapsed' => $command->collapsed, 'visible' => $command->visible, + 'created_by' => $command->created_by, ]; return array_filter($params, function ($val) { diff --git a/app/Composers/Modules/ComponentsComposer.php b/app/Composers/Modules/ComponentsComposer.php index 28b4486e753b..253713be6e75 100644 --- a/app/Composers/Modules/ComponentsComposer.php +++ b/app/Composers/Modules/ComponentsComposer.php @@ -33,14 +33,24 @@ class ComponentsComposer public function compose(View $view) { // Component & Component Group lists. - $usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id'); - + $usedComponentGroups = Component::enabled()->where('group_id', '>', 0) + ->groupBy('group_id') + ->pluck('group_id') + ; $componentGroupsBuilder = ComponentGroup::guest(); if (auth()->check()) { - $componentGroupsBuilder = ComponentGroup::loggedIn(); + $componentGroupsBuilder = ComponentGroup::loggedIn(auth()->user()); } - $componentGroups = $componentGroupsBuilder->whereIn('id', $usedComponentGroups)->orderBy('order')->get(); - $ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get(); + $componentGroups = $componentGroupsBuilder->whereIn('id', $usedComponentGroups) + ->orderBy('order') + ->get() + ; + $ungroupedComponents = Component::enabled() + ->where('group_id', 0) + ->orderBy('order') + ->orderBy('created_at') + ->get() + ; $view->withComponentGroups($componentGroups) ->withUngroupedComponents($ungroupedComponents); diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index b43d386fc940..ae9cac9870a0 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -11,14 +11,15 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; -use CachetHQ\Cachet\Bus\Commands\ComponentGroup\AddComponentGroupCommand; -use CachetHQ\Cachet\Bus\Commands\ComponentGroup\RemoveComponentGroupCommand; -use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand; -use CachetHQ\Cachet\Models\ComponentGroup; -use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Contracts\Auth\Guard; use Illuminate\Database\QueryException; use Illuminate\Support\Facades\Request; +use GrahamCampbell\Binput\Facades\Binput; +use CachetHQ\Cachet\Models\ComponentGroup; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; +use CachetHQ\Cachet\Bus\Commands\ComponentGroup\AddComponentGroupCommand; +use CachetHQ\Cachet\Bus\Commands\ComponentGroup\RemoveComponentGroupCommand; +use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand; /** * This is the component group controller. @@ -38,8 +39,8 @@ public function getGroups() { $groups = ComponentGroup::query()->guest(); - if (auth()->check()) { - $groups = ComponentGroup::query()->loggedIn(); + if (app(Guard::class)->check()) { + $groups = ComponentGroup::query()->loggedIn(app(Guard::class)->user()); } $groups->search(Binput::except(['sort', 'order', 'per_page'])); @@ -79,7 +80,8 @@ public function postGroups() Binput::get('name'), Binput::get('order', 0), Binput::get('collapsed', 0), - Binput::get('visible', 2) + Binput::get('visible', 2), + app(Guard::class)->user()->getKey() )); } catch (QueryException $e) { throw new BadRequestHttpException(); @@ -103,7 +105,8 @@ public function putGroup(ComponentGroup $group) Binput::get('name'), Binput::get('order'), Binput::get('collapsed'), - Binput::get('visible') + Binput::get('visible'), + app(Guard::class)->user()->getKey() )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 6f0f0f2a5d6e..866289d6ac81 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -278,7 +278,8 @@ public function postAddComponentGroup() Binput::get('name'), Binput::get('order', 0), Binput::get('collapsed'), - Binput::get('visible') + Binput::get('visible'), + auth()->user()->getKey() )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.add') @@ -306,7 +307,8 @@ public function updateComponentGroupAction(ComponentGroup $group) Binput::get('name'), $group->order, Binput::get('collapsed'), - Binput::get('visible') + Binput::get('visible'), + auth()->user()->getKey() )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id]) diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index 9d2d2c1746a2..e2e98e553dca 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -86,9 +86,23 @@ public function showDashboard() $components = Component::orderBy('order')->get(); $incidents = $this->getIncidents(); $subscribers = $this->getSubscribers(); - $usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id'); - $componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get(); - $ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get(); + + // Component & Component Group lists. + $usedComponentGroups = Component::enabled()->where('group_id', '>', 0) + ->groupBy('group_id') + ->pluck('group_id'); + $componentGroupsBuilder = ComponentGroup::guest(); + if (auth()->check()) { + $componentGroupsBuilder = ComponentGroup::loggedIn(auth()->user()); + } + $componentGroups = $componentGroupsBuilder->whereIn('id', $usedComponentGroups) + ->orderBy('order') + ->get(); + $ungroupedComponents = Component::enabled() + ->where('group_id', 0) + ->orderBy('order') + ->orderBy('created_at') + ->get(); $welcomeUser = !Auth::user()->welcomed; if ($welcomeUser) { diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index 746d168c0bd2..2d513c425067 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -11,6 +11,7 @@ namespace CachetHQ\Cachet\Models; +use CachetHQ\Cachet\Models\User; use AltThree\Validator\ValidatingTrait; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; @@ -44,9 +45,10 @@ class ComponentGroup extends Model implements HasPresenter * @var string */ protected $attributes = [ - 'order' => 0, - 'collapsed' => 0, - 'visible' => 0, + 'order' => 0, + 'collapsed' => 0, + 'visible' => 0, + 'created_by' => 0, ]; /** @@ -55,10 +57,11 @@ class ComponentGroup extends Model implements HasPresenter * @var string[] */ protected $casts = [ - 'name' => 'string', - 'order' => 'int', - 'collapsed' => 'int', - 'visible' => 'int', + 'name' => 'string', + 'order' => 'int', + 'collapsed' => 'int', + 'visible' => 'int', + 'created_by' => 'int', ]; /** @@ -66,7 +69,7 @@ class ComponentGroup extends Model implements HasPresenter * * @var string[] */ - protected $fillable = ['name', 'order', 'collapsed', 'visible']; + protected $fillable = ['name', 'order', 'collapsed', 'visible', 'created_by']; /** * The validation rules. @@ -74,10 +77,11 @@ class ComponentGroup extends Model implements HasPresenter * @var string[] */ public $rules = [ - 'name' => 'required|string', - 'order' => 'int', - 'collapsed' => 'int', - 'visible' => 'int', + 'name' => 'required|string', + 'order' => 'int', + 'collapsed' => 'int', + 'visible' => 'int', + 'created_by' => 'int', ]; /** @@ -163,6 +167,28 @@ public function getPresenterClass() return ComponentGroupPresenter::class; } + /** + * A group is created by an user. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function createdBy() + { + return $this->belongsTo(User::class); + } + + /** + * Was the component group created by the target user? + * + * @param User $user + * + * @return boolean + */ + public function isCreatedBy(User $user) + { + return $this->created_by === $user->getKey(); + } + /** * Finds all component groups which are visible to public. * @@ -182,8 +208,14 @@ public function scopeGuest(Builder $query) * * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeLoggedIn(Builder $query) + public function scopeLoggedIn(Builder $query, User $user) { - return $query->where('visible', '>=', self::VISIBLE_PUBLIC); + return $query->where('visible', '<', self::VISIBLE_HIDDEN) + ->orWhere(function (Builder $query) use ($user) { + $query->where('visible', self::VISIBLE_HIDDEN) + ->where('created_by', $user->getKey()) + ; + }); + ; } } diff --git a/app/Models/User.php b/app/Models/User.php index ebb838a11068..acbd53df9619 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -11,15 +11,16 @@ namespace CachetHQ\Cachet\Models; -use AltThree\Validator\ValidatingTrait; use Illuminate\Auth\Authenticatable; +use Illuminate\Support\Facades\Hash; +use AltThree\Validator\ValidatingTrait; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; +use CachetHQ\Cachet\Models\ComponentGroup; use Illuminate\Auth\Passwords\CanResetPassword; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; -use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Support\Facades\Hash; /** * This is the user model. @@ -225,4 +226,14 @@ public function getHasTwoFactorAttribute() { return trim($this->google_2fa_secret) !== ''; } + + /** + * An user has component groups. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function componentGroups() + { + return $this->hasMany(ComponentGroup::class); + } } diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index cdf54d47e40d..a6d1efe2e1ff 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -33,10 +33,11 @@ $factory->define(ComponentGroup::class, function ($faker) { return [ - 'name' => $faker->words(2, true), - 'order' => 0, - 'collapsed' => random_int(0, 3), - 'visible' => random_int(0, 2), + 'name' => $faker->words(2, true), + 'order' => 0, + 'collapsed' => random_int(0, 3), + 'visible' => random_int(0, 2), + 'created_by' => 0, ]; }); diff --git a/database/migrations/2016_08_06_100357_AlterTableComponentGroupsAddCreatedByColumn.php b/database/migrations/2016_08_06_100357_AlterTableComponentGroupsAddCreatedByColumn.php new file mode 100644 index 000000000000..50ca403a5792 --- /dev/null +++ b/database/migrations/2016_08_06_100357_AlterTableComponentGroupsAddCreatedByColumn.php @@ -0,0 +1,34 @@ +integer('created_by')->after('visible')->unsigned()->default(0); + + $table->index('created_by'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('component_groups', function (Blueprint $table) { + $table->dropColumn('created_by'); + }); + } +} diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 94e9935b3d35..e0cdc2ebcfa1 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -1,16 +1,11 @@ make(Kernel::class)->bootstrap(); return $app; } + + /** + * Sign in an user if it's the case. + * + * @param User|null $user + * + * @return AbstractTestCase + */ + protected function signIn(User $user = null) + { + if ($user) { + $this->user = $user; + } elseif (! $this->user) { + $this->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); + + try { + 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); + } catch (Exception $e) { + // + } + + return $this; + } } diff --git a/tests/Api/ComponentGroupTest.php b/tests/Api/ComponentGroupTest.php index 994ca45efdaf..29c49e9ddf00 100644 --- a/tests/Api/ComponentGroupTest.php +++ b/tests/Api/ComponentGroupTest.php @@ -11,6 +11,8 @@ namespace CachetHQ\Tests\Cachet\Api; +use CachetHQ\Cachet\Models\User; +use Illuminate\Contracts\Auth\Guard; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; @@ -25,6 +27,7 @@ class ComponentGroupTest extends AbstractApiTestCase const COMPONENT_GROUP_1_NAME = 'Component Group 1'; const COMPONENT_GROUP_2_NAME = 'Component Group 2'; const COMPONENT_GROUP_3_NAME = 'Component Group 3'; + const COMPONENT_GROUP_4_NAME = 'Component Group 4'; public function testGetGroups() { @@ -118,7 +121,7 @@ public function only_public_component_groups_are_shown_for_a_guest() public function all_component_groups_are_displayed_for_loggedin_users() { $this->createComponentGroups() - ->beUser(); + ->signIn(); $this->get('/api/v1/components/groups') ->seeJson(['name' => self::COMPONENT_GROUP_1_NAME]) @@ -127,6 +130,25 @@ public function all_component_groups_are_displayed_for_loggedin_users() $this->assertResponseOk(); } + /** @test */ + public function hidden_component_groups_arent_shown_if_not_belonging_to_loggedin_user() + { + $this->createComponentGroups() + ->signIn() + ->createComponentGroup( + self::COMPONENT_GROUP_4_NAME, + ComponentGroup::VISIBLE_HIDDEN, + $this->createUser() + ); + + $this->get('/api/v1/components/groups') + ->seeJson(['name' => self::COMPONENT_GROUP_1_NAME]) + ->seeJson(['name' => self::COMPONENT_GROUP_2_NAME]) + ->seeJson(['name' => self::COMPONENT_GROUP_3_NAME]) + ->dontSeeJson(['name' => self::COMPONENT_GROUP_4_NAME]); + $this->assertResponseOk(); + } + /** * Set up the needed data for the tests. * @@ -134,22 +156,39 @@ public function all_component_groups_are_displayed_for_loggedin_users() */ protected function createComponentGroups() { - $this->createComponentGroup(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC); - $this->createComponentGroup(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN); - $this->createComponentGroup(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN); + $this->signIn() + ->createComponentGroup(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) + ->createComponentGroup(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN) + ->createComponentGroup(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN); + + app(Guard::class)->logout(); 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 + * @param User $user + * + * @return AbstractApiTestCase */ - protected function createComponentGroup($name, $visible) + protected function createComponentGroup($name, $visible, User $user = null) { + $createdBy = 0; + if (!is_null($user)) { + $createdBy = $user->getKey(); + } elseif (!is_null($this->user)) { + $createdBy = $this->user->getKey(); + } + factory(ComponentGroup::class) - ->create(['name' => $name, 'visible' => $visible]); + ->create(['name' => $name, 'visible' => $visible, 'created_by' => $createdBy]); + + return $this; } } diff --git a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php index 87c178bed59f..03298c70f131 100644 --- a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php @@ -28,10 +28,10 @@ class AddComponentGroupCommandTest extends AbstractTestCase protected function getObjectAndParams() { - $params = ['name' => 'Test', 'order' => 0, 'collapsed' => 1, 'visible' => 2]; + $params = ['name' => 'Test', 'order' => 0, 'collapsed' => 1, 'visible' => 2, 'created_by' => 0]; $object = new AddComponentGroupCommand( - $params['name'], $params['order'], $params['collapsed'], $params['visible'] + $params['name'], $params['order'], $params['collapsed'], $params['visible'], $params['created_by'] ); return compact('params', 'object'); diff --git a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php index 4a4ff50e3ea6..71313d9111cb 100644 --- a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php @@ -29,13 +29,21 @@ class UpdateComponentGroupCommandTest extends AbstractTestCase protected function getObjectAndParams() { - $params = ['group' => new ComponentGroup(), 'name' => 'Foo', 'order' => 1, 'collapsed' => 2, 'visible' => 1]; + $params = [ + 'group' => new ComponentGroup(), + 'name' => 'Foo', + 'order' => 1, + 'collapsed' => 2, + 'visible' => 1, + 'created_by' => 0, + ]; $object = new UpdateComponentGroupCommand( $params['group'], $params['name'], $params['order'], $params['collapsed'], - $params['visible'] + $params['visible'], + $params['created_by'] ); return compact('params', 'object'); diff --git a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php new file mode 100644 index 000000000000..4721580954be --- /dev/null +++ b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php @@ -0,0 +1,102 @@ +setupPublicLoggedInAndHiddenComponentGroups() + ->setupConfig(); + } + + /** @test */ + public function on_dashboard_hidden_component_groups_are_not_displayed_if_not_belonging_to_logged_in_user() + { + $this->signIn() + ->createAComponentGroupAndAddAComponent( + self::COMPONENT_GROUP_4_NAME, + ComponentGroup::VISIBLE_HIDDEN, + $this->createUser() + ) + ; + + $this->visit('/dashboard') + ->see(self::COMPONENT_GROUP_1_NAME) + ->see(self::COMPONENT_GROUP_2_NAME) + ->see(self::COMPONENT_GROUP_3_NAME) + ->dontSee(self::COMPONENT_GROUP_4_NAME) + ; + } + + /** + * Set up the needed data for the components groups tests. + * + * @return TestCase + */ + protected function setupPublicLoggedInAndHiddenComponentGroups() + { + $this->signIn() + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN) + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN) + ; + + factory(Setting::class)->create(); + + auth()->logout(); + + return $this; + } + + /** + * Create a component group and add one component to it. + * Also attaches a creator if any given as a parameter + * or exists in the test class. + * + * @param string $name + * @param string $visible + * @param User $user + * + * @return TestCase + */ + protected function createAComponentGroupAndAddAComponent($name, $visible, User $user = null) + { + $createdBy = 0; + if (!is_null($user)) { + $createdBy = $user->getKey(); + } elseif (!is_null($this->user)) { + $createdBy = $this->user->getKey(); + } + + factory(ComponentGroup::class) + ->create(['name' => $name, 'visible' => $visible, 'created_by' => $createdBy]) + ->components() + ->save(factory(Component::class)->create()) + ; + + return $this; + } +} diff --git a/tests/Http/Controllers/StatusPageControllerTest.php b/tests/Http/Controllers/StatusPageControllerTest.php index c1851d0df861..e0fbe6476dc5 100644 --- a/tests/Http/Controllers/StatusPageControllerTest.php +++ b/tests/Http/Controllers/StatusPageControllerTest.php @@ -2,16 +2,13 @@ namespace CachetHQ\Tests\Cachet\Http\Controllers; -use Exception; +use CachetHQ\Cachet\Models\User; +use CachetHQ\Cachet\Models\Setting; +use Illuminate\Contracts\Auth\Guard; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; -use CachetHQ\Cachet\Models\Setting; -use CachetHQ\Cachet\Models\User; -use CachetHQ\Cachet\Settings\Cache; -use CachetHQ\Cachet\Settings\Repository; use CachetHQ\Tests\Cachet\AbstractTestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; -use Illuminate\Foundation\Testing\TestCase; class StatusPageControllerTest extends AbstractTestCase { @@ -20,6 +17,7 @@ class StatusPageControllerTest extends AbstractTestCase const COMPONENT_GROUP_1_NAME = 'Component Group 1'; const COMPONENT_GROUP_2_NAME = 'Component Group 2'; const COMPONENT_GROUP_3_NAME = 'Component Group 3'; + const COMPONENT_GROUP_4_NAME = 'Component Group 4'; /** * @var User @@ -30,12 +28,12 @@ protected function setUp() { parent::setUp(); - $this->setupData() + $this->setupPublicLoggedInAndHiddenComponentGroups() ->setupConfig(); } /** @test */ - public function only_public_component_groups_are_shown_for_a_guest() + public function on_index_only_public_component_groups_are_shown_to_a_guest() { $this->visit('/') ->see(self::COMPONENT_GROUP_1_NAME) @@ -44,7 +42,7 @@ public function only_public_component_groups_are_shown_for_a_guest() } /** @test */ - public function all_component_groups_are_displayed_for_loggedin_users() + public function on_index_all_component_groups_are_displayed_to_logged_in_users() { $this->signIn(); @@ -54,95 +52,67 @@ public function all_component_groups_are_displayed_for_loggedin_users() ->see(self::COMPONENT_GROUP_3_NAME); } - /** - * Set up the needed data for the tests. - * - * @return TestCase - */ - protected function setupData() + /** @test */ + public function on_index_hidden_component_groups_are_not_displayed_if_not_belonging_to_logged_in_user() { - $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC); - $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN); - $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN); - - factory(Setting::class)->create(); - - return $this; - } + $this->signIn() + ->createAComponentGroupAndAddAComponent( + self::COMPONENT_GROUP_4_NAME, + ComponentGroup::VISIBLE_HIDDEN, + $this->createUser() + ); - /** - * Create a component group and add one component to it. - * - * @param string $name - * @param string $visible - */ - protected function createAComponentGroupAndAddAComponent($name, $visible) - { - factory(ComponentGroup::class) - ->create(['name' => $name, 'visible' => $visible]) - ->components() - ->save(factory(Component::class)->create()); + $this->visit('/') + ->see(self::COMPONENT_GROUP_1_NAME) + ->see(self::COMPONENT_GROUP_2_NAME) + ->see(self::COMPONENT_GROUP_3_NAME) + ->dontSee(self::COMPONENT_GROUP_4_NAME); } /** - * Sign in an user if it's the case. - * - * @param User|null $user + * Set up the needed data for the components groups tests. * - * @return TestCase + * @return AbstractTestCase */ - protected function signIn($user = null) + protected function setupPublicLoggedInAndHiddenComponentGroups() { - if (isset($user)) { - $this->user = $user; - } + $this->signIn() + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN) + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN); - if (is_null($this->user)) { - $this->createUser(); - } + factory(Setting::class)->create(); - $this->actingAs($this->user); + app(Guard::class)->logout(); return $this; } /** - * Create an user and assign it to the user property of the class. + * Create a component group and add one component to it. + * Also attaches a creator if any given as a parameter + * or exists in the test class. * - * @return TestCase - */ - protected function createUser() - { - $this->user = factory(User::class)->create(); - - return $this; - } - - /** - * Set up the needed configuration to be able to run the tests. + * @param string $name + * @param string $visible + * @param User $user * - * @return TestCase + * @return AbstractTestCase */ - protected function setupConfig() + protected function createAComponentGroupAndAddAComponent($name, $visible, User $user = null) { - $env = $this->app->environment(); - $repo = $this->app->make(Repository::class); - $cache = $this->app->make(Cache::class); - $loaded = $cache->load($env); - - try { - 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); - } catch (Exception $e) { - // + $createdBy = 0; + if ($user) { + $createdBy = $user->getKey(); + } elseif ($this->user) { + $createdBy = $this->user->getKey(); } + factory(ComponentGroup::class) + ->create(['name' => $name, 'visible' => $visible, 'created_by' => $createdBy]) + ->components() + ->save(factory(Component::class)->create()); + return $this; } } From 1e502d8e143dc3787f4873b7ecc1703cedc68012 Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Sun, 7 Aug 2016 17:16:19 +0300 Subject: [PATCH 06/15] Replace auth() usage with app(Guard::class). --- app/Composers/Modules/ComponentsComposer.php | 16 +++++++--------- .../Dashboard/ComponentController.php | 19 ++++++++++--------- .../Dashboard/DashboardController.php | 11 ++++++----- .../Dashboard/DashboardControllerTest.php | 9 +++++---- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/app/Composers/Modules/ComponentsComposer.php b/app/Composers/Modules/ComponentsComposer.php index 253713be6e75..2ea54ec70e98 100644 --- a/app/Composers/Modules/ComponentsComposer.php +++ b/app/Composers/Modules/ComponentsComposer.php @@ -11,9 +11,10 @@ namespace CachetHQ\Cachet\Composers\Modules; +use Illuminate\Contracts\View\View; +use Illuminate\Contracts\Auth\Guard; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; -use Illuminate\Contracts\View\View; /** * This is the status page composer. @@ -35,22 +36,19 @@ public function compose(View $view) // Component & Component Group lists. $usedComponentGroups = Component::enabled()->where('group_id', '>', 0) ->groupBy('group_id') - ->pluck('group_id') - ; + ->pluck('group_id'); $componentGroupsBuilder = ComponentGroup::guest(); - if (auth()->check()) { - $componentGroupsBuilder = ComponentGroup::loggedIn(auth()->user()); + if (app(Guard::class)->check()) { + $componentGroupsBuilder = ComponentGroup::loggedIn(app(Guard::class)->user()); } $componentGroups = $componentGroupsBuilder->whereIn('id', $usedComponentGroups) ->orderBy('order') - ->get() - ; + ->get(); $ungroupedComponents = Component::enabled() ->where('group_id', 0) ->orderBy('order') ->orderBy('created_at') - ->get() - ; + ->get(); $view->withComponentGroups($componentGroups) ->withUngroupedComponents($ungroupedComponents); diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 866289d6ac81..576ce9bbd6f9 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -11,6 +11,14 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; +use CachetHQ\Cachet\Models\Tag; +use Illuminate\Routing\Controller; +use Illuminate\Contracts\Auth\Guard; +use Illuminate\Support\Facades\View; +use CachetHQ\Cachet\Models\Component; +use Illuminate\Support\Facades\Redirect; +use GrahamCampbell\Binput\Facades\Binput; +use CachetHQ\Cachet\Models\ComponentGroup; use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Bus\Commands\Component\AddComponentCommand; use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand; @@ -18,13 +26,6 @@ use CachetHQ\Cachet\Bus\Commands\ComponentGroup\AddComponentGroupCommand; use CachetHQ\Cachet\Bus\Commands\ComponentGroup\RemoveComponentGroupCommand; use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand; -use CachetHQ\Cachet\Models\Component; -use CachetHQ\Cachet\Models\ComponentGroup; -use CachetHQ\Cachet\Models\Tag; -use GrahamCampbell\Binput\Facades\Binput; -use Illuminate\Routing\Controller; -use Illuminate\Support\Facades\Redirect; -use Illuminate\Support\Facades\View; class ComponentController extends Controller { @@ -279,7 +280,7 @@ public function postAddComponentGroup() Binput::get('order', 0), Binput::get('collapsed'), Binput::get('visible'), - auth()->user()->getKey() + app(Guard::class)->user()->getKey() )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.add') @@ -308,7 +309,7 @@ public function updateComponentGroupAction(ComponentGroup $group) $group->order, Binput::get('collapsed'), Binput::get('visible'), - auth()->user()->getKey() + app(Guard::class)->user()->getKey() )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id]) diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index e2e98e553dca..28265475acb1 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -15,14 +15,15 @@ use CachetHQ\Cachet\Integrations\Contracts\Feed; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; +use Jenssegers\Date\Date; +use Illuminate\Routing\Controller; use CachetHQ\Cachet\Models\Incident; +use Illuminate\Contracts\Auth\Guard; +use Illuminate\Support\Facades\View; use CachetHQ\Cachet\Models\Subscriber; -use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redirect; -use Illuminate\Support\Facades\View; -use Jenssegers\Date\Date; /** * This is the dashboard controller class. @@ -92,8 +93,8 @@ public function showDashboard() ->groupBy('group_id') ->pluck('group_id'); $componentGroupsBuilder = ComponentGroup::guest(); - if (auth()->check()) { - $componentGroupsBuilder = ComponentGroup::loggedIn(auth()->user()); + if (app(Guard::class)->check()) { + $componentGroupsBuilder = ComponentGroup::loggedIn(app(Guard::class)->user()); } $componentGroups = $componentGroupsBuilder->whereIn('id', $usedComponentGroups) ->orderBy('order') diff --git a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php index 4721580954be..bbbcbf754ad6 100644 --- a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php +++ b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php @@ -2,13 +2,14 @@ namespace CachetHQ\Tests\Cachet\Http\Controllers; +use CachetHQ\Cachet\Models\User; +use CachetHQ\Cachet\Models\Setting; +use Illuminate\Contracts\Auth\Guard; 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; +use Illuminate\Foundation\Testing\DatabaseMigrations; class DashboardControllerTest extends AbstractTestCase { @@ -66,7 +67,7 @@ protected function setupPublicLoggedInAndHiddenComponentGroups() factory(Setting::class)->create(); - auth()->logout(); + app(Guard::class)->logout(); return $this; } From 658c76057a249c5e5fade803b810abef9d0238f9 Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Sun, 7 Aug 2016 17:46:24 +0300 Subject: [PATCH 07/15] Apply StyleCI fixes. --- .../UpdateComponentGroupCommand.php | 8 ++--- .../AddComponentGroupCommandHandler.php | 8 ++--- .../UpdateComponentGroupCommandHandler.php | 8 ++--- .../Api/ComponentGroupController.php | 10 +++---- .../Dashboard/ComponentController.php | 16 +++++----- app/Models/ComponentGroup.php | 15 ++++------ app/Models/User.php | 11 ++++--- database/factories/ModelFactory.php | 14 ++++----- ...erTableComponentGroupsAddVisibleColumn.php | 13 +++++++-- ...TableComponentGroupsAddCreatedByColumn.php | 13 +++++++-- tests/AbstractTestCase.php | 13 +++++++-- tests/Api/ComponentGroupTest.php | 4 +-- .../UpdateComponentGroupCommandTest.php | 10 +++---- .../Dashboard/DashboardControllerTest.php | 29 +++++++++++-------- .../Controllers/StatusPageControllerTest.php | 15 ++++++++-- 15 files changed, 112 insertions(+), 75 deletions(-) diff --git a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php index b92a9d155466..879708f17999 100644 --- a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php @@ -66,10 +66,10 @@ final class UpdateComponentGroupCommand * @var string[] */ public $rules = [ - 'name' => 'string', - 'order' => 'int', - 'collapsed' => 'int|between:0,3', - 'visible' => 'int|between:0,3', + 'name' => 'string', + 'order' => 'int', + 'collapsed' => 'int|between:0,3', + 'visible' => 'int|between:0,3', 'created_by' => 'int', ]; diff --git a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php index a5312d401996..a8fb3f3f131d 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php @@ -27,10 +27,10 @@ class AddComponentGroupCommandHandler public function handle(AddComponentGroupCommand $command) { $group = ComponentGroup::create([ - 'name' => $command->name, - 'order' => $command->order, - 'collapsed' => $command->collapsed, - 'visible' => $command->visible, + 'name' => $command->name, + 'order' => $command->order, + 'collapsed' => $command->collapsed, + 'visible' => $command->visible, 'created_by' => $command->created_by, ]); diff --git a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php index 96a79223e994..a770b0d4c112 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php @@ -43,10 +43,10 @@ public function handle(UpdateComponentGroupCommand $command) protected function filter(UpdateComponentGroupCommand $command) { $params = [ - 'name' => $command->name, - 'order' => $command->order, - 'collapsed' => $command->collapsed, - 'visible' => $command->visible, + 'name' => $command->name, + 'order' => $command->order, + 'collapsed' => $command->collapsed, + 'visible' => $command->visible, 'created_by' => $command->created_by, ]; diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index ae9cac9870a0..99a1e2465d82 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -11,15 +11,15 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; +use CachetHQ\Cachet\Bus\Commands\ComponentGroup\AddComponentGroupCommand; +use CachetHQ\Cachet\Bus\Commands\ComponentGroup\RemoveComponentGroupCommand; +use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand; +use CachetHQ\Cachet\Models\ComponentGroup; +use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Contracts\Auth\Guard; use Illuminate\Database\QueryException; use Illuminate\Support\Facades\Request; -use GrahamCampbell\Binput\Facades\Binput; -use CachetHQ\Cachet\Models\ComponentGroup; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; -use CachetHQ\Cachet\Bus\Commands\ComponentGroup\AddComponentGroupCommand; -use CachetHQ\Cachet\Bus\Commands\ComponentGroup\RemoveComponentGroupCommand; -use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand; /** * This is the component group controller. diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 576ce9bbd6f9..2ef80c06e11b 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -11,14 +11,6 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; -use CachetHQ\Cachet\Models\Tag; -use Illuminate\Routing\Controller; -use Illuminate\Contracts\Auth\Guard; -use Illuminate\Support\Facades\View; -use CachetHQ\Cachet\Models\Component; -use Illuminate\Support\Facades\Redirect; -use GrahamCampbell\Binput\Facades\Binput; -use CachetHQ\Cachet\Models\ComponentGroup; use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Bus\Commands\Component\AddComponentCommand; use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand; @@ -26,6 +18,14 @@ use CachetHQ\Cachet\Bus\Commands\ComponentGroup\AddComponentGroupCommand; use CachetHQ\Cachet\Bus\Commands\ComponentGroup\RemoveComponentGroupCommand; use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand; +use CachetHQ\Cachet\Models\Component; +use CachetHQ\Cachet\Models\ComponentGroup; +use CachetHQ\Cachet\Models\Tag; +use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Contracts\Auth\Guard; +use Illuminate\Routing\Controller; +use Illuminate\Support\Facades\Redirect; +use Illuminate\Support\Facades\View; class ComponentController extends Controller { diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index 2d513c425067..2f77a13730fb 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -11,14 +11,13 @@ namespace CachetHQ\Cachet\Models; -use CachetHQ\Cachet\Models\User; use AltThree\Validator\ValidatingTrait; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Builder; -use McCool\LaravelAutoPresenter\HasPresenter; -use CachetHQ\Cachet\Models\Traits\SortableTrait; use CachetHQ\Cachet\Models\Traits\SearchableTrait; +use CachetHQ\Cachet\Models\Traits\SortableTrait; use CachetHQ\Cachet\Presenters\ComponentGroupPresenter; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; +use McCool\LaravelAutoPresenter\HasPresenter; class ComponentGroup extends Model implements HasPresenter { @@ -182,7 +181,7 @@ public function createdBy() * * @param User $user * - * @return boolean + * @return bool */ public function isCreatedBy(User $user) { @@ -213,9 +212,7 @@ public function scopeLoggedIn(Builder $query, User $user) return $query->where('visible', '<', self::VISIBLE_HIDDEN) ->orWhere(function (Builder $query) use ($user) { $query->where('visible', self::VISIBLE_HIDDEN) - ->where('created_by', $user->getKey()) - ; + ->where('created_by', $user->getKey()); }); - ; } } diff --git a/app/Models/User.php b/app/Models/User.php index acbd53df9619..8a93e47db822 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -11,16 +11,15 @@ namespace CachetHQ\Cachet\Models; -use Illuminate\Auth\Authenticatable; -use Illuminate\Support\Facades\Hash; use AltThree\Validator\ValidatingTrait; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Builder; -use CachetHQ\Cachet\Models\ComponentGroup; +use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Passwords\CanResetPassword; -use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Support\Facades\Hash; /** * This is the user model. diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index a6d1efe2e1ff..5b4d724c651c 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -9,17 +9,17 @@ * file that was distributed with this source code. */ -use Carbon\Carbon; -use CachetHQ\Cachet\Models\User; +use CachetHQ\Cachet\Models\Component; +use CachetHQ\Cachet\Models\ComponentGroup; +use CachetHQ\Cachet\Models\Incident; +use CachetHQ\Cachet\Models\IncidentTemplate; use CachetHQ\Cachet\Models\Metric; +use CachetHQ\Cachet\Models\MetricPoint; use CachetHQ\Cachet\Models\Setting; -use CachetHQ\Cachet\Models\Incident; -use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Subscriber; -use CachetHQ\Cachet\Models\MetricPoint; use CachetHQ\Cachet\Models\Subscription; -use CachetHQ\Cachet\Models\ComponentGroup; -use CachetHQ\Cachet\Models\IncidentTemplate; +use CachetHQ\Cachet\Models\User; +use Carbon\Carbon; $factory->define(Component::class, function ($faker) { return [ diff --git a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php index 262fe9f224b8..fa45da06d6c0 100644 --- a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php +++ b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php @@ -1,8 +1,17 @@ make(Kernel::class)->bootstrap(); @@ -55,7 +64,7 @@ protected function signIn(User $user = null) { if ($user) { $this->user = $user; - } elseif (! $this->user) { + } elseif (!$this->user) { $this->user = $this->createUser(); } diff --git a/tests/Api/ComponentGroupTest.php b/tests/Api/ComponentGroupTest.php index 29c49e9ddf00..449550819bac 100644 --- a/tests/Api/ComponentGroupTest.php +++ b/tests/Api/ComponentGroupTest.php @@ -11,10 +11,10 @@ namespace CachetHQ\Tests\Cachet\Api; -use CachetHQ\Cachet\Models\User; -use Illuminate\Contracts\Auth\Guard; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; +use CachetHQ\Cachet\Models\User; +use Illuminate\Contracts\Auth\Guard; /** * This is the component group test class. diff --git a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php index 71313d9111cb..25ab3bcd6102 100644 --- a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php @@ -30,11 +30,11 @@ class UpdateComponentGroupCommandTest extends AbstractTestCase protected function getObjectAndParams() { $params = [ - 'group' => new ComponentGroup(), - 'name' => 'Foo', - 'order' => 1, - 'collapsed' => 2, - 'visible' => 1, + 'group' => new ComponentGroup(), + 'name' => 'Foo', + 'order' => 1, + 'collapsed' => 2, + 'visible' => 1, 'created_by' => 0, ]; $object = new UpdateComponentGroupCommand( diff --git a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php index bbbcbf754ad6..9f1234cc7e5d 100644 --- a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php +++ b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php @@ -1,15 +1,24 @@ createUser() - ) - ; + ); $this->visit('/dashboard') ->see(self::COMPONENT_GROUP_1_NAME) ->see(self::COMPONENT_GROUP_2_NAME) ->see(self::COMPONENT_GROUP_3_NAME) - ->dontSee(self::COMPONENT_GROUP_4_NAME) - ; + ->dontSee(self::COMPONENT_GROUP_4_NAME); } /** @@ -62,8 +69,7 @@ protected function setupPublicLoggedInAndHiddenComponentGroups() $this->signIn() ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN) - ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN) - ; + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN); factory(Setting::class)->create(); @@ -95,8 +101,7 @@ protected function createAComponentGroupAndAddAComponent($name, $visible, User $ factory(ComponentGroup::class) ->create(['name' => $name, 'visible' => $visible, 'created_by' => $createdBy]) ->components() - ->save(factory(Component::class)->create()) - ; + ->save(factory(Component::class)->create()); return $this; } diff --git a/tests/Http/Controllers/StatusPageControllerTest.php b/tests/Http/Controllers/StatusPageControllerTest.php index e0fbe6476dc5..c7522b9a5409 100644 --- a/tests/Http/Controllers/StatusPageControllerTest.php +++ b/tests/Http/Controllers/StatusPageControllerTest.php @@ -1,13 +1,22 @@ Date: Sat, 13 Aug 2016 17:31:50 +0300 Subject: [PATCH 08/15] Drop the hidden visibility feature and fix all tests. Some code review fixes too. --- .../AddComponentGroupCommand.php | 12 +--- .../UpdateComponentGroupCommand.php | 22 +++---- .../AddComponentGroupCommandHandler.php | 1 - .../UpdateComponentGroupCommandHandler.php | 1 - app/Composers/Modules/ComponentsComposer.php | 4 +- .../Api/ComponentGroupController.php | 4 +- .../Dashboard/ComponentController.php | 6 +- .../Dashboard/DashboardController.php | 4 +- app/Models/ComponentGroup.php | 60 +++---------------- database/factories/ModelFactory.php | 3 +- ...erTableComponentGroupsAddVisibleColumn.php | 2 +- ...TableComponentGroupsAddCreatedByColumn.php | 43 ------------- resources/lang/en/forms.php | 1 - .../dashboard/components/groups/add.blade.php | 5 +- .../components/groups/edit.blade.php | 5 +- tests/Api/ComponentGroupTest.php | 51 +++------------- .../AddComponentGroupCommandTest.php | 4 +- .../UpdateComponentGroupCommandTest.php | 4 +- .../Dashboard/DashboardControllerTest.php | 41 +++---------- .../Controllers/StatusPageControllerTest.php | 51 +++------------- 20 files changed, 58 insertions(+), 266 deletions(-) delete mode 100644 database/migrations/2016_08_06_100357_AlterTableComponentGroupsAddCreatedByColumn.php diff --git a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php index 81212cfb0aa0..96fc7e746302 100644 --- a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php @@ -46,11 +46,6 @@ final class AddComponentGroupCommand */ public $visible; - /** - * The id of the creator of the component group. - */ - public $created_by; - /** * The validation rules. * @@ -60,8 +55,7 @@ final class AddComponentGroupCommand 'name' => 'required|string', 'order' => 'int', 'collapsed' => 'int|between:0,3', - 'visible' => 'int|between:0,3', - 'created_by' => 'int', + 'visible' => 'bool', ]; /** @@ -71,16 +65,14 @@ final class AddComponentGroupCommand * @param int $order * @param int $collapsed * @param int $visible - * @param int $created_by * * @return void */ - public function __construct($name, $order, $collapsed, $visible, $created_by) + public function __construct($name, $order, $collapsed, $visible) { $this->name = $name; $this->order = (int) $order; $this->collapsed = $collapsed; $this->visible = (int) $visible; - $this->created_by = (int) $created_by; } } diff --git a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php index 879708f17999..57fd35dc473b 100644 --- a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php @@ -55,11 +55,6 @@ final class UpdateComponentGroupCommand */ public $visible; - /** - * The id of the creator of the component group. - */ - public $created_by; - /** * The validation rules. * @@ -69,29 +64,26 @@ final class UpdateComponentGroupCommand 'name' => 'string', 'order' => 'int', 'collapsed' => 'int|between:0,3', - 'visible' => 'int|between:0,3', - 'created_by' => 'int', + 'visible' => 'bool', ]; /** * Create a add component group command instance. * - * @param ComponentGroup $group - * @param string $name - * @param int $order - * @param int $collapsed - * @param into $visible - * @param int $created_by + * @param \CachetHQ\Cachet\Models\ComponentGroup $group + * @param string $name + * @param int $order + * @param int $collapsed + * @param into $visible * * @return void */ - public function __construct(ComponentGroup $group, $name, $order, $collapsed, $visible, $created_by) + public function __construct(ComponentGroup $group, $name, $order, $collapsed, $visible) { $this->group = $group; $this->name = $name; $this->order = (int) $order; $this->collapsed = $collapsed; $this->visible = (int) $visible; - $this->created_by = (int) $created_by; } } diff --git a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php index a8fb3f3f131d..89a808fdcef3 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php @@ -31,7 +31,6 @@ public function handle(AddComponentGroupCommand $command) 'order' => $command->order, 'collapsed' => $command->collapsed, 'visible' => $command->visible, - 'created_by' => $command->created_by, ]); event(new ComponentGroupWasAddedEvent($group)); diff --git a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php index a770b0d4c112..1e719fcbd843 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php @@ -47,7 +47,6 @@ protected function filter(UpdateComponentGroupCommand $command) 'order' => $command->order, 'collapsed' => $command->collapsed, 'visible' => $command->visible, - 'created_by' => $command->created_by, ]; return array_filter($params, function ($val) { diff --git a/app/Composers/Modules/ComponentsComposer.php b/app/Composers/Modules/ComponentsComposer.php index 2ea54ec70e98..f9c99ad95e14 100644 --- a/app/Composers/Modules/ComponentsComposer.php +++ b/app/Composers/Modules/ComponentsComposer.php @@ -37,9 +37,9 @@ public function compose(View $view) $usedComponentGroups = Component::enabled()->where('group_id', '>', 0) ->groupBy('group_id') ->pluck('group_id'); - $componentGroupsBuilder = ComponentGroup::guest(); + $componentGroupsBuilder = ComponentGroup::public(); if (app(Guard::class)->check()) { - $componentGroupsBuilder = ComponentGroup::loggedIn(app(Guard::class)->user()); + $componentGroupsBuilder = ComponentGroup::query(); } $componentGroups = $componentGroupsBuilder->whereIn('id', $usedComponentGroups) ->orderBy('order') diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index 99a1e2465d82..7306f0d79cd5 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -37,10 +37,10 @@ class ComponentGroupController extends AbstractApiController */ public function getGroups() { - $groups = ComponentGroup::query()->guest(); + $groups = ComponentGroup::public(); if (app(Guard::class)->check()) { - $groups = ComponentGroup::query()->loggedIn(app(Guard::class)->user()); + $groups = ComponentGroup::query(); } $groups->search(Binput::except(['sort', 'order', 'per_page'])); diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 2ef80c06e11b..1f1e9302df2e 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -279,8 +279,7 @@ public function postAddComponentGroup() Binput::get('name'), Binput::get('order', 0), Binput::get('collapsed'), - Binput::get('visible'), - app(Guard::class)->user()->getKey() + Binput::get('visible') )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.add') @@ -308,8 +307,7 @@ public function updateComponentGroupAction(ComponentGroup $group) Binput::get('name'), $group->order, Binput::get('collapsed'), - Binput::get('visible'), - app(Guard::class)->user()->getKey() + Binput::get('visible') )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id]) diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index 28265475acb1..9f8e27c24274 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -92,9 +92,9 @@ public function showDashboard() $usedComponentGroups = Component::enabled()->where('group_id', '>', 0) ->groupBy('group_id') ->pluck('group_id'); - $componentGroupsBuilder = ComponentGroup::guest(); + $componentGroupsBuilder = ComponentGroup::public(); if (app(Guard::class)->check()) { - $componentGroupsBuilder = ComponentGroup::loggedIn(app(Guard::class)->user()); + $componentGroupsBuilder = ComponentGroup::query(); } $componentGroups = $componentGroupsBuilder->whereIn('id', $usedComponentGroups) ->orderBy('order') diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index 2f77a13730fb..ca84f1e30804 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -24,19 +24,14 @@ class ComponentGroup extends Model implements HasPresenter use SearchableTrait, SortableTrait, ValidatingTrait; /** - * Viewable by public. - */ - const VISIBLE_PUBLIC = 0; - - /** - * Viewable by logged in users. + * Viewable only logged in users. */ - const VISIBLE_LOGGED_IN = 1; + const VISIBLE_LOGGED_IN = 0; /** - * Hidden. + * Viewable by public. */ - const VISIBLE_HIDDEN = 2; + const VISIBLE_PUBLIC = 1; /** * The model's attributes. @@ -47,7 +42,6 @@ class ComponentGroup extends Model implements HasPresenter 'order' => 0, 'collapsed' => 0, 'visible' => 0, - 'created_by' => 0, ]; /** @@ -60,7 +54,6 @@ class ComponentGroup extends Model implements HasPresenter 'order' => 'int', 'collapsed' => 'int', 'visible' => 'int', - 'created_by' => 'int', ]; /** @@ -68,7 +61,7 @@ class ComponentGroup extends Model implements HasPresenter * * @var string[] */ - protected $fillable = ['name', 'order', 'collapsed', 'visible', 'created_by']; + protected $fillable = ['name', 'order', 'collapsed', 'visible']; /** * The validation rules. @@ -79,8 +72,7 @@ class ComponentGroup extends Model implements HasPresenter 'name' => 'required|string', 'order' => 'int', 'collapsed' => 'int', - 'visible' => 'int', - 'created_by' => 'int', + 'visible' => 'bool', ]; /** @@ -166,28 +158,6 @@ public function getPresenterClass() return ComponentGroupPresenter::class; } - /** - * A group is created by an user. - * - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo - */ - public function createdBy() - { - return $this->belongsTo(User::class); - } - - /** - * Was the component group created by the target user? - * - * @param User $user - * - * @return bool - */ - public function isCreatedBy(User $user) - { - return $this->created_by === $user->getKey(); - } - /** * Finds all component groups which are visible to public. * @@ -195,24 +165,8 @@ public function isCreatedBy(User $user) * * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeGuest(Builder $query) + public function scopePublic(Builder $query) { return $query->where('visible', self::VISIBLE_PUBLIC); } - - /** - * Finds all component groups which are only visible to logged in users. - * - * @param \Illuminate\Database\Eloquent\Builder $query - * - * @return \Illuminate\Database\Eloquent\Builder - */ - public function scopeLoggedIn(Builder $query, User $user) - { - return $query->where('visible', '<', self::VISIBLE_HIDDEN) - ->orWhere(function (Builder $query) use ($user) { - $query->where('visible', self::VISIBLE_HIDDEN) - ->where('created_by', $user->getKey()); - }); - } } diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 5b4d724c651c..7188c299209f 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -36,8 +36,7 @@ 'name' => $faker->words(2, true), 'order' => 0, 'collapsed' => random_int(0, 3), - 'visible' => random_int(0, 2), - 'created_by' => 0, + 'visible' => $faker->boolean(), ]; }); diff --git a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php index fa45da06d6c0..ecff053460e1 100644 --- a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php +++ b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php @@ -23,7 +23,7 @@ class AlterTableComponentGroupsAddVisibleColumn extends Migration public function up() { Schema::table('component_groups', function (Blueprint $table) { - $table->integer('visible')->after('order')->unsigned()->default(2); + $table->integer('visible')->after('order')->unsigned()->default(0); $table->index('visible'); }); diff --git a/database/migrations/2016_08_06_100357_AlterTableComponentGroupsAddCreatedByColumn.php b/database/migrations/2016_08_06_100357_AlterTableComponentGroupsAddCreatedByColumn.php deleted file mode 100644 index 78ee66bade7d..000000000000 --- a/database/migrations/2016_08_06_100357_AlterTableComponentGroupsAddCreatedByColumn.php +++ /dev/null @@ -1,43 +0,0 @@ -integer('created_by')->after('visible')->unsigned()->default(0); - - $table->index('created_by'); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::table('component_groups', function (Blueprint $table) { - $table->dropColumn('created_by'); - }); - } -} diff --git a/resources/lang/en/forms.php b/resources/lang/en/forms.php index 30e99dd23a71..f73059ccce5a 100644 --- a/resources/lang/en/forms.php +++ b/resources/lang/en/forms.php @@ -85,7 +85,6 @@ 'visibility' => 'Visibility', 'visibility_public' => 'Visible to public', 'visibility_authenticated' => 'Visible only to logged in users', - 'visibility_hidden' => 'Hidden', ], ], diff --git a/resources/views/dashboard/components/groups/add.blade.php b/resources/views/dashboard/components/groups/add.blade.php index 2295e213c3fd..9effa5799210 100644 --- a/resources/views/dashboard/components/groups/add.blade.php +++ b/resources/views/dashboard/components/groups/add.blade.php @@ -32,9 +32,8 @@
diff --git a/resources/views/dashboard/components/groups/edit.blade.php b/resources/views/dashboard/components/groups/edit.blade.php index 833e38a3c5a5..e7aacb9f5907 100644 --- a/resources/views/dashboard/components/groups/edit.blade.php +++ b/resources/views/dashboard/components/groups/edit.blade.php @@ -32,9 +32,8 @@
diff --git a/tests/Api/ComponentGroupTest.php b/tests/Api/ComponentGroupTest.php index 449550819bac..957e07d70296 100644 --- a/tests/Api/ComponentGroupTest.php +++ b/tests/Api/ComponentGroupTest.php @@ -13,7 +13,6 @@ use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; -use CachetHQ\Cachet\Models\User; use Illuminate\Contracts\Auth\Guard; /** @@ -26,8 +25,6 @@ class ComponentGroupTest extends AbstractApiTestCase { const COMPONENT_GROUP_1_NAME = 'Component Group 1'; const COMPONENT_GROUP_2_NAME = 'Component Group 2'; - const COMPONENT_GROUP_3_NAME = 'Component Group 3'; - const COMPONENT_GROUP_4_NAME = 'Component Group 4'; public function testGetGroups() { @@ -70,8 +67,9 @@ public function testPostGroup() 'name' => 'Foo', 'order' => 1, 'collapsed' => 1, + 'visible' => ComponentGroup::VISIBLE_PUBLIC, ]); - $this->seeJson(['name' => 'Foo', 'order' => 1, 'collapsed' => 1]); + $this->seeJson(['name' => 'Foo', 'order' => 1, 'collapsed' => 1, 'visible' => ComponentGroup::VISIBLE_PUBLIC]); $this->assertResponseOk(); } @@ -112,8 +110,7 @@ public function only_public_component_groups_are_shown_for_a_guest() $this->get('/api/v1/components/groups') ->seeJson(['name' => self::COMPONENT_GROUP_1_NAME]) - ->dontSeeJson(['name' => self::COMPONENT_GROUP_2_NAME]) - ->dontSeeJson(['name' => self::COMPONENT_GROUP_3_NAME]); + ->dontSeeJson(['name' => self::COMPONENT_GROUP_2_NAME]); $this->assertResponseOk(); } @@ -125,27 +122,7 @@ public function all_component_groups_are_displayed_for_loggedin_users() $this->get('/api/v1/components/groups') ->seeJson(['name' => self::COMPONENT_GROUP_1_NAME]) - ->seeJson(['name' => self::COMPONENT_GROUP_2_NAME]) - ->seeJson(['name' => self::COMPONENT_GROUP_3_NAME]); - $this->assertResponseOk(); - } - - /** @test */ - public function hidden_component_groups_arent_shown_if_not_belonging_to_loggedin_user() - { - $this->createComponentGroups() - ->signIn() - ->createComponentGroup( - self::COMPONENT_GROUP_4_NAME, - ComponentGroup::VISIBLE_HIDDEN, - $this->createUser() - ); - - $this->get('/api/v1/components/groups') - ->seeJson(['name' => self::COMPONENT_GROUP_1_NAME]) - ->seeJson(['name' => self::COMPONENT_GROUP_2_NAME]) - ->seeJson(['name' => self::COMPONENT_GROUP_3_NAME]) - ->dontSeeJson(['name' => self::COMPONENT_GROUP_4_NAME]); + ->seeJson(['name' => self::COMPONENT_GROUP_2_NAME]); $this->assertResponseOk(); } @@ -156,12 +133,8 @@ public function hidden_component_groups_arent_shown_if_not_belonging_to_loggedin */ protected function createComponentGroups() { - $this->signIn() - ->createComponentGroup(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) - ->createComponentGroup(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN) - ->createComponentGroup(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN); - - app(Guard::class)->logout(); + $this->createComponentGroup(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) + ->createComponentGroup(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN); return $this; } @@ -173,21 +146,13 @@ protected function createComponentGroups() * * @param string $name * @param string $visible - * @param User $user * * @return AbstractApiTestCase */ - protected function createComponentGroup($name, $visible, User $user = null) + protected function createComponentGroup($name, $visible) { - $createdBy = 0; - if (!is_null($user)) { - $createdBy = $user->getKey(); - } elseif (!is_null($this->user)) { - $createdBy = $this->user->getKey(); - } - factory(ComponentGroup::class) - ->create(['name' => $name, 'visible' => $visible, 'created_by' => $createdBy]); + ->create(['name' => $name, 'visible' => $visible]); return $this; } diff --git a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php index 03298c70f131..87c178bed59f 100644 --- a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php @@ -28,10 +28,10 @@ class AddComponentGroupCommandTest extends AbstractTestCase protected function getObjectAndParams() { - $params = ['name' => 'Test', 'order' => 0, 'collapsed' => 1, 'visible' => 2, 'created_by' => 0]; + $params = ['name' => 'Test', 'order' => 0, 'collapsed' => 1, 'visible' => 2]; $object = new AddComponentGroupCommand( - $params['name'], $params['order'], $params['collapsed'], $params['visible'], $params['created_by'] + $params['name'], $params['order'], $params['collapsed'], $params['visible'] ); return compact('params', 'object'); diff --git a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php index 25ab3bcd6102..de55499acd80 100644 --- a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php @@ -35,15 +35,13 @@ protected function getObjectAndParams() 'order' => 1, 'collapsed' => 2, 'visible' => 1, - 'created_by' => 0, ]; $object = new UpdateComponentGroupCommand( $params['group'], $params['name'], $params['order'], $params['collapsed'], - $params['visible'], - $params['created_by'] + $params['visible'] ); return compact('params', 'object'); diff --git a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php index 9f1234cc7e5d..a41c4500a199 100644 --- a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php +++ b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php @@ -26,8 +26,6 @@ class DashboardControllerTest extends AbstractTestCase const COMPONENT_GROUP_1_NAME = 'Component Group 1'; const COMPONENT_GROUP_2_NAME = 'Component Group 2'; - const COMPONENT_GROUP_3_NAME = 'Component Group 3'; - const COMPONENT_GROUP_4_NAME = 'Component Group 4'; /** * @var User @@ -38,25 +36,18 @@ protected function setUp() { parent::setUp(); - $this->setupPublicLoggedInAndHiddenComponentGroups() + $this->setupPublicAndNonPublicComponentGroups() ->setupConfig(); } /** @test */ - public function on_dashboard_hidden_component_groups_are_not_displayed_if_not_belonging_to_logged_in_user() + public function on_dashboard_all_component_groups_are_displayed() { - $this->signIn() - ->createAComponentGroupAndAddAComponent( - self::COMPONENT_GROUP_4_NAME, - ComponentGroup::VISIBLE_HIDDEN, - $this->createUser() - ); + $this->signIn(); $this->visit('/dashboard') ->see(self::COMPONENT_GROUP_1_NAME) - ->see(self::COMPONENT_GROUP_2_NAME) - ->see(self::COMPONENT_GROUP_3_NAME) - ->dontSee(self::COMPONENT_GROUP_4_NAME); + ->see(self::COMPONENT_GROUP_2_NAME); } /** @@ -64,42 +55,28 @@ public function on_dashboard_hidden_component_groups_are_not_displayed_if_not_be * * @return TestCase */ - protected function setupPublicLoggedInAndHiddenComponentGroups() + protected function setupPublicAndNonPublicComponentGroups() { - $this->signIn() - ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) - ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN) - ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN); + $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN); factory(Setting::class)->create(); - app(Guard::class)->logout(); - return $this; } /** * Create a component group and add one component to it. - * Also attaches a creator if any given as a parameter - * or exists in the test class. * * @param string $name * @param string $visible - * @param User $user * * @return TestCase */ - protected function createAComponentGroupAndAddAComponent($name, $visible, User $user = null) + protected function createAComponentGroupAndAddAComponent($name, $visible) { - $createdBy = 0; - if (!is_null($user)) { - $createdBy = $user->getKey(); - } elseif (!is_null($this->user)) { - $createdBy = $this->user->getKey(); - } - factory(ComponentGroup::class) - ->create(['name' => $name, 'visible' => $visible, 'created_by' => $createdBy]) + ->create(['name' => $name, 'visible' => $visible]) ->components() ->save(factory(Component::class)->create()); diff --git a/tests/Http/Controllers/StatusPageControllerTest.php b/tests/Http/Controllers/StatusPageControllerTest.php index c7522b9a5409..3c859cd0a235 100644 --- a/tests/Http/Controllers/StatusPageControllerTest.php +++ b/tests/Http/Controllers/StatusPageControllerTest.php @@ -25,8 +25,6 @@ class StatusPageControllerTest extends AbstractTestCase const COMPONENT_GROUP_1_NAME = 'Component Group 1'; const COMPONENT_GROUP_2_NAME = 'Component Group 2'; - const COMPONENT_GROUP_3_NAME = 'Component Group 3'; - const COMPONENT_GROUP_4_NAME = 'Component Group 4'; /** * @var User @@ -37,7 +35,7 @@ protected function setUp() { parent::setUp(); - $this->setupPublicLoggedInAndHiddenComponentGroups() + $this->setupPublicAndNonPublicComponentGroups() ->setupConfig(); } @@ -46,8 +44,7 @@ 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) - ->dontSee(self::COMPONENT_GROUP_3_NAME); + ->dontSee(self::COMPONENT_GROUP_2_NAME); } /** @test */ @@ -57,25 +54,7 @@ public function on_index_all_component_groups_are_displayed_to_logged_in_users() $this->visit('/') ->see(self::COMPONENT_GROUP_1_NAME) - ->see(self::COMPONENT_GROUP_2_NAME) - ->see(self::COMPONENT_GROUP_3_NAME); - } - - /** @test */ - public function on_index_hidden_component_groups_are_not_displayed_if_not_belonging_to_logged_in_user() - { - $this->signIn() - ->createAComponentGroupAndAddAComponent( - self::COMPONENT_GROUP_4_NAME, - ComponentGroup::VISIBLE_HIDDEN, - $this->createUser() - ); - - $this->visit('/') - ->see(self::COMPONENT_GROUP_1_NAME) - ->see(self::COMPONENT_GROUP_2_NAME) - ->see(self::COMPONENT_GROUP_3_NAME) - ->dontSee(self::COMPONENT_GROUP_4_NAME); + ->see(self::COMPONENT_GROUP_2_NAME); } /** @@ -83,42 +62,28 @@ public function on_index_hidden_component_groups_are_not_displayed_if_not_belong * * @return AbstractTestCase */ - protected function setupPublicLoggedInAndHiddenComponentGroups() + protected function setupPublicAndNonPublicComponentGroups() { - $this->signIn() - ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) - ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN) - ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_3_NAME, ComponentGroup::VISIBLE_HIDDEN); + $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN); factory(Setting::class)->create(); - app(Guard::class)->logout(); - return $this; } /** * Create a component group and add one component to it. - * Also attaches a creator if any given as a parameter - * or exists in the test class. * * @param string $name * @param string $visible - * @param User $user * * @return AbstractTestCase */ - protected function createAComponentGroupAndAddAComponent($name, $visible, User $user = null) + protected function createAComponentGroupAndAddAComponent($name, $visible) { - $createdBy = 0; - if ($user) { - $createdBy = $user->getKey(); - } elseif ($this->user) { - $createdBy = $this->user->getKey(); - } - factory(ComponentGroup::class) - ->create(['name' => $name, 'visible' => $visible, 'created_by' => $createdBy]) + ->create(['name' => $name, 'visible' => $visible]) ->components() ->save(factory(Component::class)->create()); From 1cfb26b8c6afde4cc32d3490cb4409acf41b13b9 Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Sat, 13 Aug 2016 17:47:30 +0300 Subject: [PATCH 09/15] Rename public to visible since it's a reserved keyword. Apply StyleCI fixes and correct typo. --- app/Composers/Modules/ComponentsComposer.php | 6 +++--- app/Http/Controllers/Api/ComponentGroupController.php | 2 +- app/Http/Controllers/Dashboard/ComponentController.php | 1 - app/Http/Controllers/Dashboard/DashboardController.php | 10 +++++----- app/Models/ComponentGroup.php | 6 +++++- tests/Api/ComponentGroupTest.php | 1 - .../Controllers/Dashboard/DashboardControllerTest.php | 1 - tests/Http/Controllers/StatusPageControllerTest.php | 1 - 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/Composers/Modules/ComponentsComposer.php b/app/Composers/Modules/ComponentsComposer.php index f9c99ad95e14..515f47a48101 100644 --- a/app/Composers/Modules/ComponentsComposer.php +++ b/app/Composers/Modules/ComponentsComposer.php @@ -11,10 +11,10 @@ namespace CachetHQ\Cachet\Composers\Modules; -use Illuminate\Contracts\View\View; -use Illuminate\Contracts\Auth\Guard; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; +use Illuminate\Contracts\Auth\Guard; +use Illuminate\Contracts\View\View; /** * This is the status page composer. @@ -37,7 +37,7 @@ public function compose(View $view) $usedComponentGroups = Component::enabled()->where('group_id', '>', 0) ->groupBy('group_id') ->pluck('group_id'); - $componentGroupsBuilder = ComponentGroup::public(); + $componentGroupsBuilder = ComponentGroup::visible(); if (app(Guard::class)->check()) { $componentGroupsBuilder = ComponentGroup::query(); } diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index 7306f0d79cd5..a70a638ce439 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -37,7 +37,7 @@ class ComponentGroupController extends AbstractApiController */ public function getGroups() { - $groups = ComponentGroup::public(); + $groups = ComponentGroup::visible(); if (app(Guard::class)->check()) { $groups = ComponentGroup::query(); diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 1f1e9302df2e..6f0f0f2a5d6e 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -22,7 +22,6 @@ use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\Tag; use GrahamCampbell\Binput\Facades\Binput; -use Illuminate\Contracts\Auth\Guard; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\View; diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index 9f8e27c24274..d134c113fc5e 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -15,15 +15,15 @@ use CachetHQ\Cachet\Integrations\Contracts\Feed; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; -use Jenssegers\Date\Date; -use Illuminate\Routing\Controller; use CachetHQ\Cachet\Models\Incident; -use Illuminate\Contracts\Auth\Guard; -use Illuminate\Support\Facades\View; use CachetHQ\Cachet\Models\Subscriber; +use Illuminate\Contracts\Auth\Guard; +use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redirect; +use Illuminate\Support\Facades\View; +use Jenssegers\Date\Date; /** * This is the dashboard controller class. @@ -92,7 +92,7 @@ public function showDashboard() $usedComponentGroups = Component::enabled()->where('group_id', '>', 0) ->groupBy('group_id') ->pluck('group_id'); - $componentGroupsBuilder = ComponentGroup::public(); + $componentGroupsBuilder = ComponentGroup::visible(); if (app(Guard::class)->check()) { $componentGroupsBuilder = ComponentGroup::query(); } diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index ca84f1e30804..d77550687b88 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -25,11 +25,15 @@ class ComponentGroup extends Model implements HasPresenter /** * Viewable only logged in users. + * + * @var int */ const VISIBLE_LOGGED_IN = 0; /** * Viewable by public. + * + * @var int */ const VISIBLE_PUBLIC = 1; @@ -165,7 +169,7 @@ public function getPresenterClass() * * @return \Illuminate\Database\Eloquent\Builder */ - public function scopePublic(Builder $query) + public function scopeVisible(Builder $query) { return $query->where('visible', self::VISIBLE_PUBLIC); } diff --git a/tests/Api/ComponentGroupTest.php b/tests/Api/ComponentGroupTest.php index 957e07d70296..1e24e75ce74d 100644 --- a/tests/Api/ComponentGroupTest.php +++ b/tests/Api/ComponentGroupTest.php @@ -13,7 +13,6 @@ use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; -use Illuminate\Contracts\Auth\Guard; /** * This is the component group test class. diff --git a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php index a41c4500a199..63b87f50d206 100644 --- a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php +++ b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php @@ -16,7 +16,6 @@ use CachetHQ\Cachet\Models\Setting; use CachetHQ\Cachet\Models\User; use CachetHQ\Tests\Cachet\AbstractTestCase; -use Illuminate\Contracts\Auth\Guard; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\TestCase; diff --git a/tests/Http/Controllers/StatusPageControllerTest.php b/tests/Http/Controllers/StatusPageControllerTest.php index 3c859cd0a235..210ce292d6be 100644 --- a/tests/Http/Controllers/StatusPageControllerTest.php +++ b/tests/Http/Controllers/StatusPageControllerTest.php @@ -16,7 +16,6 @@ use CachetHQ\Cachet\Models\Setting; use CachetHQ\Cachet\Models\User; use CachetHQ\Tests\Cachet\AbstractTestCase; -use Illuminate\Contracts\Auth\Guard; use Illuminate\Foundation\Testing\DatabaseMigrations; class StatusPageControllerTest extends AbstractTestCase From 1cf11f1084f7eea014bdc9a5a94bfbf6540cbe46 Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Sat, 10 Sep 2016 14:10:29 +0300 Subject: [PATCH 10/15] Code review changes. --- app/Console/Commands/DemoSeederCommand.php | 2 ++ .../Api/ComponentGroupController.php | 18 +++++++++++++----- ...terTableComponentGroupsAddVisibleColumn.php | 1 + .../dashboard/components/groups/add.blade.php | 1 - tests/AbstractTestCase.php | 6 +----- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/Console/Commands/DemoSeederCommand.php b/app/Console/Commands/DemoSeederCommand.php index a682ddd84a96..6b2f13274a99 100644 --- a/app/Console/Commands/DemoSeederCommand.php +++ b/app/Console/Commands/DemoSeederCommand.php @@ -106,10 +106,12 @@ protected function seedComponentGroups() 'name' => 'Websites', 'order' => 1, 'collapsed' => 0, + 'visible' => ComponentGroup::VISIBLE_LOGGED_IN, ], [ 'name' => 'Alt Three', 'order' => 2, 'collapsed' => 1, + 'visible' => ComponentGroup::VISIBLE_PUBLIC, ], ]; diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index a70a638ce439..cf043e9fa970 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -30,6 +30,16 @@ */ class ComponentGroupController extends AbstractApiController { + protected $guard; + + /** + * @param Guard $guard + */ + public function __construct(Guard $guard) + { + $this->guard = $guard; + } + /** * Get all groups. * @@ -39,7 +49,7 @@ public function getGroups() { $groups = ComponentGroup::visible(); - if (app(Guard::class)->check()) { + if ($this->guard->check()) { $groups = ComponentGroup::query(); } @@ -80,8 +90,7 @@ public function postGroups() Binput::get('name'), Binput::get('order', 0), Binput::get('collapsed', 0), - Binput::get('visible', 2), - app(Guard::class)->user()->getKey() + Binput::get('visible', 2) )); } catch (QueryException $e) { throw new BadRequestHttpException(); @@ -105,8 +114,7 @@ public function putGroup(ComponentGroup $group) Binput::get('name'), Binput::get('order'), Binput::get('collapsed'), - Binput::get('visible'), - app(Guard::class)->user()->getKey() + Binput::get('visible') )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php index ecff053460e1..0f810d1ebea3 100644 --- a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php +++ b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php @@ -26,6 +26,7 @@ public function up() $table->integer('visible')->after('order')->unsigned()->default(0); $table->index('visible'); + $table->engine = 'InnoDB'; }); } diff --git a/resources/views/dashboard/components/groups/add.blade.php b/resources/views/dashboard/components/groups/add.blade.php index 9effa5799210..2b6923a15c52 100644 --- a/resources/views/dashboard/components/groups/add.blade.php +++ b/resources/views/dashboard/components/groups/add.blade.php @@ -36,7 +36,6 @@
-
diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 3f1c6acf7bd3..4ba9de85b9f9 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -62,11 +62,7 @@ public function createApplication() */ protected function signIn(User $user = null) { - if ($user) { - $this->user = $user; - } elseif (!$this->user) { - $this->user = $this->createUser(); - } + $this->user = $user ?: $this->createUser(); $this->be($this->user); From bc3cfeafa2abacad0e3b3a3bc46afe6c1e059607 Mon Sep 17 00:00:00 2001 From: Marius Palade Date: Sun, 11 Sep 2016 20:59:33 +0300 Subject: [PATCH 11/15] Tidy up component and component groups gathering. --- app/Composers/Modules/ComponentsComposer.php | 21 ++++---------- .../Dashboard/DashboardController.php | 21 ++++---------- app/Models/Component.php | 29 +++++++++++++++++++ app/Models/ComponentGroup.php | 20 +++++++++++++ 4 files changed, 61 insertions(+), 30 deletions(-) diff --git a/app/Composers/Modules/ComponentsComposer.php b/app/Composers/Modules/ComponentsComposer.php index 515f47a48101..02581c57be38 100644 --- a/app/Composers/Modules/ComponentsComposer.php +++ b/app/Composers/Modules/ComponentsComposer.php @@ -34,21 +34,12 @@ class ComponentsComposer public function compose(View $view) { // Component & Component Group lists. - $usedComponentGroups = Component::enabled()->where('group_id', '>', 0) - ->groupBy('group_id') - ->pluck('group_id'); - $componentGroupsBuilder = ComponentGroup::visible(); - if (app(Guard::class)->check()) { - $componentGroupsBuilder = ComponentGroup::query(); - } - $componentGroups = $componentGroupsBuilder->whereIn('id', $usedComponentGroups) - ->orderBy('order') - ->get(); - $ungroupedComponents = Component::enabled() - ->where('group_id', 0) - ->orderBy('order') - ->orderBy('created_at') - ->get(); + $usedComponentGroups = Component::usedGroups()->pluck('group_id'); + $componentGroups = ComponentGroup::visibleUsed( + $usedComponentGroups, + app(Guard::class)->check() + )->get(); + $ungroupedComponents = Component::ungroupped()->get(); $view->withComponentGroups($componentGroups) ->withUngroupedComponents($ungroupedComponents); diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index d134c113fc5e..92a379bbc943 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -89,21 +89,12 @@ public function showDashboard() $subscribers = $this->getSubscribers(); // Component & Component Group lists. - $usedComponentGroups = Component::enabled()->where('group_id', '>', 0) - ->groupBy('group_id') - ->pluck('group_id'); - $componentGroupsBuilder = ComponentGroup::visible(); - if (app(Guard::class)->check()) { - $componentGroupsBuilder = ComponentGroup::query(); - } - $componentGroups = $componentGroupsBuilder->whereIn('id', $usedComponentGroups) - ->orderBy('order') - ->get(); - $ungroupedComponents = Component::enabled() - ->where('group_id', 0) - ->orderBy('order') - ->orderBy('created_at') - ->get(); + $usedComponentGroups = Component::usedGroups()->pluck('group_id'); + $componentGroups = ComponentGroup::visibleUsed( + $usedComponentGroups, + app(Guard::class)->check() + )->get(); + $ungroupedComponents = Component::ungroupped()->get(); $welcomeUser = !Auth::user()->welcomed; if ($welcomeUser) { diff --git a/app/Models/Component.php b/app/Models/Component.php index dd87b64639d7..c166f1625e1e 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -188,6 +188,35 @@ public function scopeDisabled(Builder $query) return $query->where('enabled', false); } + /** + * Finds all ungroupped components. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeUngroupped(Builder $query) + { + return $query->enabled() + ->where('group_id', 0) + ->orderBy('order') + ->orderBy('created_at'); + } + + /** + * Finds all used component groups. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeUsedGroups(Builder $query) + { + return $query->enabled() + ->where('group_id', '>', 0) + ->groupBy('group_id'); + } + /** * Returns all of the tags on this component. * diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index d77550687b88..ad0aaefc6bd0 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -17,6 +17,7 @@ use CachetHQ\Cachet\Presenters\ComponentGroupPresenter; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Collection; use McCool\LaravelAutoPresenter\HasPresenter; class ComponentGroup extends Model implements HasPresenter @@ -173,4 +174,23 @@ public function scopeVisible(Builder $query) { return $query->where('visible', self::VISIBLE_PUBLIC); } + + /** + * Finds all used and visible component groups. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Support\Collection $usedComponentGroups + * @param bool $isAuthenticated + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeVisibleUsed(Builder $query, Collection $usedComponentGroups, $isAuthenticated) + { + if (!$isAuthenticated) { + $query->visible(); + } + + return $query->whereIn('id', $usedComponentGroups) + ->orderBy('order'); + } } From 8e3e4014fa73f2514f144b6cc4849ab79409aa8a Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Tue, 13 Sep 2016 12:16:04 +0300 Subject: [PATCH 12/15] Code review changes and StyleCI fixes. --- .../UpdateComponentGroupCommand.php | 2 +- app/Composers/Modules/ComponentsComposer.php | 49 ++++++++++++++++--- .../Api/ComponentGroupController.php | 11 ++++- .../Dashboard/DashboardController.php | 39 ++++++++++++--- app/Models/Component.php | 8 +-- app/Models/ComponentGroup.php | 9 +--- app/Models/User.php | 2 +- database/factories/ModelFactory.php | 8 +-- ...erTableComponentGroupsAddVisibleColumn.php | 1 - tests/AbstractTestCase.php | 17 +++---- .../AddComponentGroupCommandTest.php | 8 ++- .../UpdateComponentGroupCommandTest.php | 2 +- 12 files changed, 107 insertions(+), 49 deletions(-) diff --git a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php index 57fd35dc473b..69a3324ab893 100644 --- a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php @@ -74,7 +74,7 @@ final class UpdateComponentGroupCommand * @param string $name * @param int $order * @param int $collapsed - * @param into $visible + * @param int $visible * * @return void */ diff --git a/app/Composers/Modules/ComponentsComposer.php b/app/Composers/Modules/ComponentsComposer.php index 02581c57be38..8239d38cdaf0 100644 --- a/app/Composers/Modules/ComponentsComposer.php +++ b/app/Composers/Modules/ComponentsComposer.php @@ -24,6 +24,25 @@ */ 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. * @@ -34,14 +53,28 @@ class ComponentsComposer public function compose(View $view) { // Component & Component Group lists. - $usedComponentGroups = Component::usedGroups()->pluck('group_id'); - $componentGroups = ComponentGroup::visibleUsed( - $usedComponentGroups, - app(Guard::class)->check() - )->get(); - $ungroupedComponents = Component::ungroupped()->get(); - - $view->withComponentGroups($componentGroups) + $groupedComponents = $this->getVisibleGroupedComponents(); + $ungroupedComponents = Component::ungrouped()->get(); + + $view->withComponentGroups($groupedComponents) ->withUngroupedComponents($ungroupedComponents); } + + /** + * Get visible grouped components. + * + * @return \Illuminate\Support\Collection + */ + protected function getVisibleGroupedComponents() + { + $componentGroupsBuilder = ComponentGroup::visible(); + if ($this->guard->check()) { + $componentGroupsBuilder = ComponentGroup::query(); + } + + $usedComponentGroups = Component::grouped()->pluck('group_id'); + + return $componentGroupsBuilder->used($usedComponentGroups) + ->get(); + } } diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index cf043e9fa970..c1a074caf670 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -30,10 +30,17 @@ */ class ComponentGroupController extends AbstractApiController { + /** + * The user session object. + * + * @var \Illuminate\Contracts\Auth\Guard + */ protected $guard; /** - * @param Guard $guard + * Creates a new component group controller instance. + * + * @param \Illuminate\Contracts\Auth\Guard $guard */ public function __construct(Guard $guard) { @@ -90,7 +97,7 @@ public function postGroups() Binput::get('name'), Binput::get('order', 0), Binput::get('collapsed', 0), - Binput::get('visible', 2) + Binput::get('visible', ComponentGroup::VISIBLE_LOGGED_IN) )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index 92a379bbc943..4a80bacd0c9d 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -53,16 +53,25 @@ class DashboardController extends Controller */ protected $feed; + /** + * The user session object. + * + * @var \Illuminate\Contracts\Auth\Guard + */ + protected $guard; + /** * Creates a new dashboard controller instance. * * @param \CachetHQ\Cachet\Integrations\Feed $feed + * @param \Illuminate\Contracts\Auth\Guard $guard * * @return void */ - public function __construct(Feed $feed) + public function __construct(Feed $feed, Guard $guard) { $this->feed = $feed; + $this->guard = $guard; $this->startDate = new Date(); $this->dateTimeZone = Config::get('cachet.timezone'); } @@ -89,12 +98,8 @@ public function showDashboard() $subscribers = $this->getSubscribers(); // Component & Component Group lists. - $usedComponentGroups = Component::usedGroups()->pluck('group_id'); - $componentGroups = ComponentGroup::visibleUsed( - $usedComponentGroups, - app(Guard::class)->check() - )->get(); - $ungroupedComponents = Component::ungroupped()->get(); + $groupedComponents = $this->getVisibleGroupedComponents(); + $ungroupedComponents = Component::ungrouped()->get(); $welcomeUser = !Auth::user()->welcomed; if ($welcomeUser) { @@ -112,7 +117,7 @@ public function showDashboard() ->withIncidents($incidents) ->withSubscribers($subscribers) ->withEntries($entries) - ->withComponentGroups($componentGroups) + ->withComponentGroups($groupedComponents) ->withUngroupedComponents($ungroupedComponents) ->withWelcomeUser($welcomeUser); } @@ -180,4 +185,22 @@ protected function getSubscribers() return $allSubscribers; } + + /** + * Get visible grouped components. + * + * @return \Illuminate\Support\Collection + */ + protected function getVisibleGroupedComponents() + { + $componentGroupsBuilder = ComponentGroup::visible(); + if ($this->guard->check()) { + $componentGroupsBuilder = ComponentGroup::query(); + } + + $usedComponentGroups = Component::grouped()->pluck('group_id'); + + return $componentGroupsBuilder->used($usedComponentGroups) + ->get(); + } } diff --git a/app/Models/Component.php b/app/Models/Component.php index c166f1625e1e..98f5c899c28d 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -189,13 +189,13 @@ public function scopeDisabled(Builder $query) } /** - * Finds all ungroupped components. + * Finds all ungrouped components. * * @param \Illuminate\Database\Eloquent\Builder $query * * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeUngroupped(Builder $query) + public function scopeUngrouped(Builder $query) { return $query->enabled() ->where('group_id', 0) @@ -204,13 +204,13 @@ public function scopeUngroupped(Builder $query) } /** - * Finds all used component groups. + * Finds all grouped components. * * @param \Illuminate\Database\Eloquent\Builder $query * * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeUsedGroups(Builder $query) + public function scopeGrouped(Builder $query) { return $query->enabled() ->where('group_id', '>', 0) diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index ad0aaefc6bd0..155093766456 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -176,20 +176,15 @@ public function scopeVisible(Builder $query) } /** - * Finds all used and visible component groups. + * Finds all used component groups. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Support\Collection $usedComponentGroups - * @param bool $isAuthenticated * * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeVisibleUsed(Builder $query, Collection $usedComponentGroups, $isAuthenticated) + public function scopeUsed(Builder $query, Collection $usedComponentGroups) { - if (!$isAuthenticated) { - $query->visible(); - } - return $query->whereIn('id', $usedComponentGroups) ->orderBy('order'); } diff --git a/app/Models/User.php b/app/Models/User.php index 8a93e47db822..e85721ea25de 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -227,7 +227,7 @@ public function getHasTwoFactorAttribute() } /** - * An user has component groups. + * A user has many component groups. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 7188c299209f..de96e720d384 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -33,10 +33,10 @@ $factory->define(ComponentGroup::class, function ($faker) { return [ - 'name' => $faker->words(2, true), - 'order' => 0, - 'collapsed' => random_int(0, 3), - 'visible' => $faker->boolean(), + 'name' => $faker->words(2, true), + 'order' => 0, + 'collapsed' => random_int(0, 3), + 'visible' => $faker->boolean(), ]; }); diff --git a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php index 0f810d1ebea3..ecff053460e1 100644 --- a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php +++ b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php @@ -26,7 +26,6 @@ public function up() $table->integer('visible')->after('order')->unsigned()->default(0); $table->index('visible'); - $table->engine = 'InnoDB'; }); } diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 4ba9de85b9f9..5901a1e62484 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -14,7 +14,6 @@ use CachetHQ\Cachet\Models\User; use CachetHQ\Cachet\Settings\Cache; use CachetHQ\Cachet\Settings\Repository; -use Exception; use Illuminate\Contracts\Console\Kernel; use Illuminate\Foundation\Testing\TestCase; @@ -93,18 +92,14 @@ protected function setupConfig() $cache = $this->app->make(Cache::class); $loaded = $cache->load($env); - try { - if ($loaded === false) { - $loaded = $repo->all(); - $cache->store($env, $loaded); - } + if ($loaded === false) { + $loaded = $repo->all(); + $cache->store($env, $loaded); + } - $settings = array_merge($this->app->config->get('setting'), $loaded); + $settings = array_merge($this->app->config->get('setting'), $loaded); - $this->app->config->set('setting', $settings); - } catch (Exception $e) { - // - } + $this->app->config->set('setting', $settings); return $this; } diff --git a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php index 87c178bed59f..f88c67ec170c 100644 --- a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php @@ -14,6 +14,7 @@ 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,7 +29,12 @@ class AddComponentGroupCommandTest extends AbstractTestCase protected function getObjectAndParams() { - $params = ['name' => 'Test', 'order' => 0, 'collapsed' => 1, 'visible' => 2]; + $params = [ + 'name' => 'Test', + 'order' => 0, + 'collapsed' => 1, + 'visible' => ComponentGroup::VISIBLE_LOGGED_IN, + ]; $object = new AddComponentGroupCommand( $params['name'], $params['order'], $params['collapsed'], $params['visible'] diff --git a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php index de55499acd80..36f96a50fd23 100644 --- a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php @@ -34,7 +34,7 @@ protected function getObjectAndParams() 'name' => 'Foo', 'order' => 1, 'collapsed' => 2, - 'visible' => 1, + 'visible' => ComponentGroup::VISIBLE_LOGGED_IN, ]; $object = new UpdateComponentGroupCommand( $params['group'], From 7de032beeb451a30555b416ca000a197bd271ba4 Mon Sep 17 00:00:00 2001 From: Marius Palade Date: Sat, 17 Sep 2016 11:23:38 +0300 Subject: [PATCH 13/15] Code review changes. --- .../AddComponentGroupCommand.php | 10 +++---- .../UpdateComponentGroupCommand.php | 10 +++---- .../AddComponentGroupCommandHandler.php | 8 ++--- .../UpdateComponentGroupCommandHandler.php | 8 ++--- app/Composers/Modules/ComponentsComposer.php | 13 ++++---- app/Console/Commands/DemoSeederCommand.php | 4 +-- .../Api/ComponentGroupController.php | 9 +++--- .../Dashboard/DashboardController.php | 11 ++++--- app/Models/ComponentGroup.php | 30 +++++++++---------- ...erTableComponentGroupsAddVisibleColumn.php | 5 +++- tests/Api/ComponentGroupTest.php | 10 +++---- .../AddComponentGroupCommandTest.php | 2 +- .../UpdateComponentGroupCommandTest.php | 2 +- .../Dashboard/DashboardControllerTest.php | 4 +-- .../Controllers/StatusPageControllerTest.php | 4 +-- 15 files changed, 65 insertions(+), 65 deletions(-) diff --git a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php index 96fc7e746302..2982e495c18b 100644 --- a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php @@ -40,7 +40,7 @@ final class AddComponentGroupCommand public $collapsed; /** - * To whom should the component group be visible? + * Is the component visible to public? * * @var int */ @@ -52,10 +52,10 @@ final class AddComponentGroupCommand * @var string[] */ public $rules = [ - 'name' => 'required|string', - 'order' => 'int', - 'collapsed' => 'int|between:0,3', - 'visible' => 'bool', + 'name' => 'required|string', + 'order' => 'int', + 'collapsed' => 'int|between:0,3', + 'visible' => 'bool', ]; /** diff --git a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php index 69a3324ab893..9f1dbd96fa78 100644 --- a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php @@ -49,7 +49,7 @@ final class UpdateComponentGroupCommand public $collapsed; /** - * To whom should the component group be visible? + * Is the component visible to public? * * @var int */ @@ -61,10 +61,10 @@ final class UpdateComponentGroupCommand * @var string[] */ public $rules = [ - 'name' => 'string', - 'order' => 'int', - 'collapsed' => 'int|between:0,3', - 'visible' => 'bool', + 'name' => 'string', + 'order' => 'int', + 'collapsed' => 'int|between:0,3', + 'visible' => 'bool', ]; /** diff --git a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php index 89a808fdcef3..b2a935a86e97 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php @@ -27,10 +27,10 @@ class AddComponentGroupCommandHandler public function handle(AddComponentGroupCommand $command) { $group = ComponentGroup::create([ - 'name' => $command->name, - 'order' => $command->order, - 'collapsed' => $command->collapsed, - 'visible' => $command->visible, + 'name' => $command->name, + 'order' => $command->order, + 'collapsed' => $command->collapsed, + 'visible' => $command->visible, ]); event(new ComponentGroupWasAddedEvent($group)); diff --git a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php index 1e719fcbd843..ef0f3a07ef88 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php @@ -43,10 +43,10 @@ public function handle(UpdateComponentGroupCommand $command) protected function filter(UpdateComponentGroupCommand $command) { $params = [ - 'name' => $command->name, - 'order' => $command->order, - 'collapsed' => $command->collapsed, - 'visible' => $command->visible, + 'name' => $command->name, + 'order' => $command->order, + 'collapsed' => $command->collapsed, + 'visible' => $command->visible, ]; return array_filter($params, function ($val) { diff --git a/app/Composers/Modules/ComponentsComposer.php b/app/Composers/Modules/ComponentsComposer.php index 8239d38cdaf0..b581e0457641 100644 --- a/app/Composers/Modules/ComponentsComposer.php +++ b/app/Composers/Modules/ComponentsComposer.php @@ -32,7 +32,7 @@ class ComponentsComposer protected $guard; /** - * Creates a new Components composer instance. + * Creates a new components composer instance. * * @param \Illuminate\Contracts\Auth\Guard $guard * @@ -52,11 +52,10 @@ public function __construct(Guard $guard) */ public function compose(View $view) { - // Component & Component Group lists. - $groupedComponents = $this->getVisibleGroupedComponents(); + $componentGroups = $this->getVisibleGroupedComponents(); $ungroupedComponents = Component::ungrouped()->get(); - $view->withComponentGroups($groupedComponents) + $view->withComponentGroups($componentGroups) ->withUngroupedComponents($ungroupedComponents); } @@ -67,9 +66,9 @@ public function compose(View $view) */ protected function getVisibleGroupedComponents() { - $componentGroupsBuilder = ComponentGroup::visible(); - if ($this->guard->check()) { - $componentGroupsBuilder = ComponentGroup::query(); + $componentGroupsBuilder = ComponentGroup::query(); + if (!$this->guard->check()) { + $componentGroupsBuilder->visible(); } $usedComponentGroups = Component::grouped()->pluck('group_id'); diff --git a/app/Console/Commands/DemoSeederCommand.php b/app/Console/Commands/DemoSeederCommand.php index 6b2f13274a99..283f6d6182c1 100644 --- a/app/Console/Commands/DemoSeederCommand.php +++ b/app/Console/Commands/DemoSeederCommand.php @@ -106,12 +106,12 @@ protected function seedComponentGroups() 'name' => 'Websites', 'order' => 1, 'collapsed' => 0, - 'visible' => ComponentGroup::VISIBLE_LOGGED_IN, + 'visible' => ComponentGroup::VISIBLE_AUTHENTICATED, ], [ 'name' => 'Alt Three', 'order' => 2, 'collapsed' => 1, - 'visible' => ComponentGroup::VISIBLE_PUBLIC, + 'visible' => ComponentGroup::VISIBLE_GUEST, ], ]; diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index c1a074caf670..4804511deb2b 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -54,10 +54,9 @@ public function __construct(Guard $guard) */ public function getGroups() { - $groups = ComponentGroup::visible(); - - if ($this->guard->check()) { - $groups = ComponentGroup::query(); + $groups = ComponentGroup::query(); + if (!$this->guard->check()) { + $groups = ComponentGroup::visible(); } $groups->search(Binput::except(['sort', 'order', 'per_page'])); @@ -97,7 +96,7 @@ public function postGroups() Binput::get('name'), Binput::get('order', 0), Binput::get('collapsed', 0), - Binput::get('visible', ComponentGroup::VISIBLE_LOGGED_IN) + Binput::get('visible', ComponentGroup::VISIBLE_AUTHENTICATED) )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index 4a80bacd0c9d..94d07c476492 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -97,8 +97,7 @@ public function showDashboard() $incidents = $this->getIncidents(); $subscribers = $this->getSubscribers(); - // Component & Component Group lists. - $groupedComponents = $this->getVisibleGroupedComponents(); + $componentGroups = $this->getVisibleGroupedComponents(); $ungroupedComponents = Component::ungrouped()->get(); $welcomeUser = !Auth::user()->welcomed; @@ -117,7 +116,7 @@ public function showDashboard() ->withIncidents($incidents) ->withSubscribers($subscribers) ->withEntries($entries) - ->withComponentGroups($groupedComponents) + ->withComponentGroups($componentGroups) ->withUngroupedComponents($ungroupedComponents) ->withWelcomeUser($welcomeUser); } @@ -193,9 +192,9 @@ protected function getSubscribers() */ protected function getVisibleGroupedComponents() { - $componentGroupsBuilder = ComponentGroup::visible(); - if ($this->guard->check()) { - $componentGroupsBuilder = ComponentGroup::query(); + $componentGroupsBuilder = ComponentGroup::query(); + if (!$this->guard->check()) { + $componentGroupsBuilder = ComponentGroup::visible(); } $usedComponentGroups = Component::grouped()->pluck('group_id'); diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index 155093766456..5f881dbda338 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -25,18 +25,18 @@ class ComponentGroup extends Model implements HasPresenter use SearchableTrait, SortableTrait, ValidatingTrait; /** - * Viewable only logged in users. + * Viewable only authenticated users. * * @var int */ - const VISIBLE_LOGGED_IN = 0; + const VISIBLE_AUTHENTICATED = 0; /** * Viewable by public. * * @var int */ - const VISIBLE_PUBLIC = 1; + const VISIBLE_GUEST = 1; /** * The model's attributes. @@ -44,9 +44,9 @@ class ComponentGroup extends Model implements HasPresenter * @var string */ protected $attributes = [ - 'order' => 0, - 'collapsed' => 0, - 'visible' => 0, + 'order' => 0, + 'collapsed' => 0, + 'visible' => 0, ]; /** @@ -55,10 +55,10 @@ class ComponentGroup extends Model implements HasPresenter * @var string[] */ protected $casts = [ - 'name' => 'string', - 'order' => 'int', - 'collapsed' => 'int', - 'visible' => 'int', + 'name' => 'string', + 'order' => 'int', + 'collapsed' => 'int', + 'visible' => 'int', ]; /** @@ -74,10 +74,10 @@ class ComponentGroup extends Model implements HasPresenter * @var string[] */ public $rules = [ - 'name' => 'required|string', - 'order' => 'int', - 'collapsed' => 'int', - 'visible' => 'bool', + 'name' => 'required|string', + 'order' => 'int', + 'collapsed' => 'int', + 'visible' => 'bool', ]; /** @@ -172,7 +172,7 @@ public function getPresenterClass() */ public function scopeVisible(Builder $query) { - return $query->where('visible', self::VISIBLE_PUBLIC); + return $query->where('visible', self::VISIBLE_GUEST); } /** diff --git a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php index ecff053460e1..4d933d3ebc23 100644 --- a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php +++ b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php @@ -23,7 +23,10 @@ class AlterTableComponentGroupsAddVisibleColumn extends Migration public function up() { Schema::table('component_groups', function (Blueprint $table) { - $table->integer('visible')->after('order')->unsigned()->default(0); + $table->tinyInteger('visible') + ->after('order') + ->unsigned() + ->default(\CachetHQ\Cachet\Models\ComponentGroup::VISIBLE_AUTHENTICATED); $table->index('visible'); }); diff --git a/tests/Api/ComponentGroupTest.php b/tests/Api/ComponentGroupTest.php index 1e24e75ce74d..2fa40fe62668 100644 --- a/tests/Api/ComponentGroupTest.php +++ b/tests/Api/ComponentGroupTest.php @@ -28,7 +28,7 @@ class ComponentGroupTest extends AbstractApiTestCase public function testGetGroups() { $groups = factory('CachetHQ\Cachet\Models\ComponentGroup', 3) - ->create(['visible' => ComponentGroup::VISIBLE_PUBLIC]); + ->create(['visible' => ComponentGroup::VISIBLE_GUEST]); $this->get('/api/v1/components/groups'); $this->seeJson(['id' => $groups[0]->id]); @@ -66,9 +66,9 @@ public function testPostGroup() 'name' => 'Foo', 'order' => 1, 'collapsed' => 1, - 'visible' => ComponentGroup::VISIBLE_PUBLIC, + 'visible' => ComponentGroup::VISIBLE_GUEST, ]); - $this->seeJson(['name' => 'Foo', 'order' => 1, 'collapsed' => 1, 'visible' => ComponentGroup::VISIBLE_PUBLIC]); + $this->seeJson(['name' => 'Foo', 'order' => 1, 'collapsed' => 1, 'visible' => ComponentGroup::VISIBLE_GUEST]); $this->assertResponseOk(); } @@ -132,8 +132,8 @@ public function all_component_groups_are_displayed_for_loggedin_users() */ protected function createComponentGroups() { - $this->createComponentGroup(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) - ->createComponentGroup(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN); + $this->createComponentGroup(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_GUEST) + ->createComponentGroup(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_AUTHENTICATED); return $this; } diff --git a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php index f88c67ec170c..a828cee5ac34 100644 --- a/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/AddComponentGroupCommandTest.php @@ -33,7 +33,7 @@ protected function getObjectAndParams() 'name' => 'Test', 'order' => 0, 'collapsed' => 1, - 'visible' => ComponentGroup::VISIBLE_LOGGED_IN, + 'visible' => ComponentGroup::VISIBLE_AUTHENTICATED, ]; $object = new AddComponentGroupCommand( diff --git a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php index 36f96a50fd23..0eab4189ef68 100644 --- a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php @@ -34,7 +34,7 @@ protected function getObjectAndParams() 'name' => 'Foo', 'order' => 1, 'collapsed' => 2, - 'visible' => ComponentGroup::VISIBLE_LOGGED_IN, + 'visible' => ComponentGroup::VISIBLE_AUTHENTICATED, ]; $object = new UpdateComponentGroupCommand( $params['group'], diff --git a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php index 63b87f50d206..d05096c6c34e 100644 --- a/tests/Http/Controllers/Dashboard/DashboardControllerTest.php +++ b/tests/Http/Controllers/Dashboard/DashboardControllerTest.php @@ -56,8 +56,8 @@ public function on_dashboard_all_component_groups_are_displayed() */ protected function setupPublicAndNonPublicComponentGroups() { - $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) - ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN); + $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_GUEST) + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_AUTHENTICATED); factory(Setting::class)->create(); diff --git a/tests/Http/Controllers/StatusPageControllerTest.php b/tests/Http/Controllers/StatusPageControllerTest.php index 210ce292d6be..6f9a3c68e1f7 100644 --- a/tests/Http/Controllers/StatusPageControllerTest.php +++ b/tests/Http/Controllers/StatusPageControllerTest.php @@ -63,8 +63,8 @@ public function on_index_all_component_groups_are_displayed_to_logged_in_users() */ protected function setupPublicAndNonPublicComponentGroups() { - $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_PUBLIC) - ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_LOGGED_IN); + $this->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_1_NAME, ComponentGroup::VISIBLE_GUEST) + ->createAComponentGroupAndAddAComponent(self::COMPONENT_GROUP_2_NAME, ComponentGroup::VISIBLE_AUTHENTICATED); factory(Setting::class)->create(); From 09b4570c3a3399843d194e7f164813bb3886b34b Mon Sep 17 00:00:00 2001 From: Marius Palade Date: Sun, 18 Sep 2016 13:52:11 +0300 Subject: [PATCH 14/15] Remove extra whitespace --- .../ComponentGroup/UpdateComponentGroupCommandTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php index 0eab4189ef68..3ee166a38b43 100644 --- a/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php +++ b/tests/Bus/Commands/ComponentGroup/UpdateComponentGroupCommandTest.php @@ -30,11 +30,11 @@ class UpdateComponentGroupCommandTest extends AbstractTestCase protected function getObjectAndParams() { $params = [ - 'group' => new ComponentGroup(), - 'name' => 'Foo', - 'order' => 1, - 'collapsed' => 2, - 'visible' => ComponentGroup::VISIBLE_AUTHENTICATED, + 'group' => new ComponentGroup(), + 'name' => 'Foo', + 'order' => 1, + 'collapsed' => 2, + 'visible' => ComponentGroup::VISIBLE_AUTHENTICATED, ]; $object = new UpdateComponentGroupCommand( $params['group'], From 259672f575d757ae77d149be0c9faf99da148939 Mon Sep 17 00:00:00 2001 From: Yoyosan Date: Mon, 19 Sep 2016 14:52:09 +0300 Subject: [PATCH 15/15] Remove useless method. --- app/Models/User.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index e85721ea25de..ebb838a11068 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -225,14 +225,4 @@ public function getHasTwoFactorAttribute() { return trim($this->google_2fa_secret) !== ''; } - - /** - * A user has many component groups. - * - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ - public function componentGroups() - { - return $this->hasMany(ComponentGroup::class); - } }