diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b2d632..8f6db4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# Version 1.7.2 +Fix an issue with `SeatGroupsController` where an unused variable blocked the edit-view to be loaded. + # Version 1.7.1 This new version has two main purposes: 1. introduce testing diff --git a/Homestead.yaml b/Homestead.yaml new file mode 100644 index 0000000..7f1fe00 --- /dev/null +++ b/Homestead.yaml @@ -0,0 +1,20 @@ +ip: 192.168.10.10 +memory: 2048 +cpus: 2 +provider: virtualbox +authorize: ~/.ssh/id_rsa.pub +keys: + - ~/.ssh/id_rsa +folders: + - + map: /Users/fehu/PhpstormProjects/seat-groups + to: /home/vagrant/code +sites: + - map: homestead.test + to: /home/vagrant/code/public + - map: redis + to: /home/vagrant/code/public +databases: + - homestead +name: seat-groups +hostname: seat-groups diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..5f4ffba --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,53 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +require 'json' +require 'yaml' + +VAGRANTFILE_API_VERSION ||= "2" +confDir = $confDir ||= File.expand_path("vendor/laravel/homestead", File.dirname(__FILE__)) + +homesteadYamlPath = File.expand_path("Homestead.yaml", File.dirname(__FILE__)) +homesteadJsonPath = File.expand_path("Homestead.json", File.dirname(__FILE__)) +afterScriptPath = "after.sh" +customizationScriptPath = "user-customizations.sh" +aliasesPath = "aliases" + +require File.expand_path(confDir + '/scripts/homestead.rb') + +Vagrant.require_version '>= 1.9.0' + +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + if File.exist? aliasesPath then + config.vm.provision "file", source: aliasesPath, destination: "/tmp/bash_aliases" + config.vm.provision "shell" do |s| + s.inline = "awk '{ sub(\"\r$\", \"\"); print }' /tmp/bash_aliases > /home/vagrant/.bash_aliases" + end + end + + if File.exist? homesteadYamlPath then + settings = YAML::load(File.read(homesteadYamlPath)) + elsif File.exist? homesteadJsonPath then + settings = JSON::parse(File.read(homesteadJsonPath)) + else + abort "Homestead settings file not found in " + File.dirname(__FILE__) + end + + Homestead.configure(config, settings) + + if File.exist? afterScriptPath then + config.vm.provision "shell", path: afterScriptPath, privileged: false, keep_color: true + end + + if File.exist? customizationScriptPath then + config.vm.provision "shell", path: customizationScriptPath, privileged: false, keep_color: true + end + + if Vagrant.has_plugin?('vagrant-hostsupdater') + config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] } + elsif Vagrant.has_plugin?('vagrant-hostmanager') + config.hostmanager.enabled = true + config.hostmanager.manage_host = true + config.hostmanager.aliases = settings['sites'].map { |site| site['map'] } + end +end diff --git a/after.sh b/after.sh new file mode 100644 index 0000000..1eefa74 --- /dev/null +++ b/after.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +# If you would like to do some extra provisioning you may +# add any commands you wish to this file and they will +# be run after the Homestead machine is provisioned. +# +# If you have user-specific configurations you would like +# to apply, you may also create user-customizations.sh, +# which will be run after this script. diff --git a/aliases b/aliases new file mode 100644 index 0000000..d7a40b6 --- /dev/null +++ b/aliases @@ -0,0 +1,264 @@ +alias ..="cd .." +alias ...="cd ../.." + +alias h='cd ~' +alias c='clear' +alias art=artisan + +alias phpspec='vendor/bin/phpspec' +alias phpunit='vendor/bin/phpunit' +alias serve=serve-laravel + +alias xoff='sudo phpdismod -s cli xdebug' +alias xon='sudo phpenmod -s cli xdebug' + +function artisan() { + php artisan "$@" +} + +function dusk() { + pids=$(pidof /usr/bin/Xvfb) + + if [ ! -n "$pids" ]; then + Xvfb :0 -screen 0 1280x960x24 & + fi + + php artisan dusk "$@" +} + +function php71() { + sudo update-alternatives --set php /usr/bin/php7.1 + sudo update-alternatives --set php-config /usr/bin/php-config7.1 + sudo update-alternatives --set phpize /usr/bin/phpize7.1 +} + +function php72() { + sudo update-alternatives --set php /usr/bin/php7.2 + sudo update-alternatives --set php-config /usr/bin/php-config7.2 + sudo update-alternatives --set phpize /usr/bin/phpize7.2 +} + +function php73() { + sudo update-alternatives --set php /usr/bin/php7.3 + sudo update-alternatives --set php-config /usr/bin/php-config7.3 + sudo update-alternatives --set phpize /usr/bin/phpize7.3 +} + +function serve-apache() { + if [[ "$1" && "$2" ]] + then + sudo bash /vagrant/vendor/laravel/homestead/scripts/create-certificate.sh "$1" + sudo dos2unix /vagrant/vendor/laravel/homestead/scripts/serve-apache.sh + sudo bash /vagrant/vendor/laravel/homestead/scripts/serve-apache.sh "$1" "$2" 80 443 "${3:-7.1}" + else + echo "Error: missing required parameters." + echo "Usage: " + echo " serve-apache domain path" + fi +} + +function serve-laravel() { + if [[ "$1" && "$2" ]] + then + sudo bash /vagrant/vendor/laravel/homestead/scripts/create-certificate.sh "$1" + sudo dos2unix /vagrant/vendor/laravel/homestead/scripts/serve-laravel.sh + sudo bash /vagrant/vendor/laravel/homestead/scripts/serve-laravel.sh "$1" "$2" 80 443 "${3:-7.1}" + else + echo "Error: missing required parameters." + echo "Usage: " + echo " serve domain path" + fi +} + +function serve-proxy() { + if [[ "$1" && "$2" ]] + then + sudo dos2unix /vagrant/vendor/laravel/homestead/scripts/serve-proxy.sh + sudo bash /vagrant/vendor/laravel/homestead/scripts/serve-proxy.sh "$1" "$2" 80 443 "${3:-7.1}" + else + echo "Error: missing required parameters." + echo "Usage: " + echo " serve-proxy domain port" + fi +} + +function serve-silverstripe() { + if [[ "$1" && "$2" ]] + then + sudo bash /vagrant/vendor/laravel/homestead/scripts/create-certificate.sh "$1" + sudo dos2unix /vagrant/vendor/laravel/homestead/scripts/serve-silverstripe.sh + sudo bash /vagrant/vendor/laravel/homestead/scripts/serve-silverstripe.sh "$1" "$2" 80 443 "${3:-7.1}" + else + echo "Error: missing required parameters." + echo "Usage: " + echo " serve-silverstripe domain path" + fi +} + +function serve-spa() { + if [[ "$1" && "$2" ]] + then + sudo bash /vagrant/vendor/laravel/homestead/scripts/create-certificate.sh "$1" + sudo dos2unix /vagrant/vendor/laravel/homestead/scripts/serve-spa.sh + sudo bash /vagrant/vendor/laravel/homestead/scripts/serve-spa.sh "$1" "$2" 80 443 "${3:-7.1}" + else + echo "Error: missing required parameters." + echo "Usage: " + echo " serve-spa domain path" + fi +} + +function serve-statamic() { + if [[ "$1" && "$2" ]] + then + sudo bash /vagrant/vendor/laravel/homestead/scripts/create-certificate.sh "$1" + sudo dos2unix /vagrant/vendor/laravel/homestead/scripts/serve-statamic.sh + sudo bash /vagrant/vendor/laravel/homestead/scripts/serve-statamic.sh "$1" "$2" 80 443 "${3:-7.1}" + else + echo "Error: missing required parameters." + echo "Usage: " + echo " serve-statamic domain path" + fi +} + +function serve-symfony2() { + if [[ "$1" && "$2" ]] + then + sudo bash /vagrant/vendor/laravel/homestead/scripts/create-certificate.sh "$1" + sudo dos2unix /vagrant/vendor/laravel/homestead/scripts/serve-symfony2.sh + sudo bash /vagrant/vendor/laravel/homestead/scripts/serve-symfony2.sh "$1" "$2" 80 443 "${3:-7.1}" + else + echo "Error: missing required parameters." + echo "Usage: " + echo " serve-symfony2 domain path" + fi +} + +function serve-symfony4() { + if [[ "$1" && "$2" ]] + then + sudo bash /vagrant/vendor/laravel/homestead/scripts/create-certificate.sh "$1" + sudo dos2unix /vagrant/vendor/laravel/homestead/scripts/serve-symfony4.sh + sudo bash /vagrant/vendor/laravel/homestead/scripts/serve-symfony4.sh "$1" "$2" 80 443 "${3:-7.1}" + else + echo "Error: missing required parameters." + echo "Usage: " + echo " serve-symfony4 domain path" + fi +} + +function serve-pimcore() { + if [[ "$1" && "$2" ]] + then + sudo bash /vagrant/vendor/laravel/homestead/scripts/create-certificate.sh "$1" + sudo dos2unix /vagrant/vendor/laravel/homestead/scripts/serve-pimcore.sh + sudo bash /vagrant/vendor/laravel/homestead/scripts/serve-pimcore.sh "$1" "$2" 80 443 "${3:-7.1}" + else + echo "Error: missing required parameters." + echo "Usage: " + echo " serve-pimcore domain path" + fi +} + +function share() { + if [[ "$1" ]] + then + ngrok http ${@:2} -host-header="$1" 80 + else + echo "Error: missing required parameters." + echo "Usage: " + echo " share domain" + echo "Invocation with extra params passed directly to ngrok" + echo " share domain -region=eu -subdomain=test1234" + fi +} + +function flip() { + sudo bash /vagrant/vendor/laravel/homestead/scripts/flip-webserver.sh +} + +function __has_pv() { + $(hash pv 2>/dev/null); + + return $? +} + +function __pv_install_message() { + if ! __has_pv; then + echo $1 + echo "Install pv with \`sudo apt-get install -y pv\` then run this command again." + echo "" + fi +} + +function dbexport() { + FILE=${1:-/vagrant/mysqldump.sql.gz} + + # This gives an estimate of the size of the SQL file + # It appears that 80% is a good approximation of + # the ratio of estimated size to actual size + SIZE_QUERY="select ceil(sum(data_length) * 0.8) as size from information_schema.TABLES" + + __pv_install_message "Want to see export progress?" + + echo "Exporting databases to '$FILE'" + + if __has_pv; then + ADJUSTED_SIZE=$(mysql --vertical -uhomestead -psecret -e "$SIZE_QUERY" 2>/dev/null | grep 'size' | awk '{print $2}') + HUMAN_READABLE_SIZE=$(numfmt --to=iec-i --suffix=B --format="%.3f" $ADJUSTED_SIZE) + + echo "Estimated uncompressed size: $HUMAN_READABLE_SIZE" + mysqldump -uhomestead -psecret --all-databases --skip-lock-tables --routines 2>/dev/null | pv --size=$ADJUSTED_SIZE | gzip > "$FILE" + else + mysqldump -uhomestead -psecret --all-databases --skip-lock-tables --routines 2>/dev/null | gzip > "$FILE" + fi + + echo "Done." +} + +function dbimport() { + FILE=${1:-/vagrant/mysqldump.sql.gz} + + __pv_install_message "Want to see import progress?" + + echo "Importing databases from '$FILE'" + + if __has_pv; then + pv "$FILE" --progress --eta | zcat | mysql -uhomestead -psecret 2>/dev/null + else + cat "$FILE" | zcat | mysql -uhomestead -psecret 2>/dev/null + fi + + echo "Done." +} + +function xphp() { + (php -m | grep -q xdebug) + if [[ $? -eq 0 ]] + then + XDEBUG_ENABLED=true + else + XDEBUG_ENABLED=false + fi + + if ! $XDEBUG_ENABLED; then xon; fi + + php \ + -dxdebug.remote_host=192.168.10.1 \ + -dxdebug.remote_autostart=1 \ + "$@" + + if ! $XDEBUG_ENABLED; then xoff; fi +} + +function update-socket-wrench() { + cd /var/www/socket-wrench + git pull origin release + composer install + php artisan migrate --force +} + +function seed-socket-wrench() { + cd /var/www/socket-wrench + php artisan db:seed +} diff --git a/composer.json b/composer.json index 9b09962..d50286a 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "orchestra/database" : "~3.5", "phpunit/phpunit": "~6.0", "spatie/phpunit-watcher": "^1.8", - "codeclimate/php-test-reporter": "^0.4.4" + "codeclimate/php-test-reporter": "^0.4.4", + "laravel/homestead": "^8.5" }, "conflict": { "herpaderpaldent/seat-notifications": "<2.0.0" diff --git a/src/Http/Controllers/SeatGroupsController.php b/src/Http/Controllers/SeatGroupsController.php index e55eefa..78e1520 100644 --- a/src/Http/Controllers/SeatGroupsController.php +++ b/src/Http/Controllers/SeatGroupsController.php @@ -110,31 +110,37 @@ public function show($id) /** * Show the form for editing the specified resource. * - * @param int $id + * @param int $id * * @param \Herpaderpaldent\Seat\SeatGroups\Actions\Corporations\GetCorporationListAction $action * + * @param \Herpaderpaldent\Seat\SeatGroups\Actions\Alliances\GetAllianceListAction $get_alliance_list_action + * * @return \Illuminate\Http\Response */ public function edit($id, GetCorporationListAction $action, GetAllianceListAction $get_alliance_list_action) { + $all_available_alliances = $get_alliance_list_action->execute([ - 'seatgroup_id' =>$id, + 'seatgroup_id' => $id, ]); + $all_corporations = $action->execute([ - 'seatgroup_id' =>$id, - 'origin' => 'SeatGroupsController', + 'seatgroup_id' => $id, + 'origin' => 'SeatGroupsController', ]); + $all_corporations_for_title = $action->execute([ - 'seatgroup_id' =>$id, - 'origin' => 'corporation-tile-form', + 'seatgroup_id' => $id, + 'origin' => 'corporation-tile-form', ]); + $roles = Role::all(); $seatgroup = SeatGroup::find($id); $available_seatgroups = SeatGroup::whereNotIn('id', $seatgroup->children->pluck('id')->push($id)->toArray())->get(); $all_groups = Group::all(); - return view('seatgroups::edit', compact('seatgroup', 'id', 'all_corporations', 'roles', 'corporations', 'all_groups', 'all_corporations_for_title', 'available_seatgroups', 'all_available_alliances')); + return view('seatgroups::edit', compact('seatgroup', 'id', 'all_corporations', 'roles', 'all_groups', 'all_corporations_for_title', 'available_seatgroups', 'all_available_alliances')); } /** diff --git a/src/config/seatgroups.config.php b/src/config/seatgroups.config.php index 6db51b6..6fc8327 100644 --- a/src/config/seatgroups.config.php +++ b/src/config/seatgroups.config.php @@ -24,7 +24,7 @@ */ return [ - 'version' => '1.7.1', + 'version' => '1.7.2', ]; //TODO: Update Version diff --git a/tests/SeatGroupIndexTest.php b/tests/SeatGroupIndexTest.php index d6d767f..fb4a7f6 100644 --- a/tests/SeatGroupIndexTest.php +++ b/tests/SeatGroupIndexTest.php @@ -3,18 +3,98 @@ namespace Herpaderpaldent\Seat\SeatGroups\Test; +use Herpaderpaldent\Seat\SeatGroups\Models\SeatGroup; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\RefreshDatabase; +use Seat\Services\Models\UserSetting; +use Seat\Web\Models\Acl\Permission; +use Seat\Web\Models\Acl\Role; class SeatGroupIndexTest extends TestCase { - /** - * A basic test example. - * - * @return void - */ - public function testExample() + + /** @test */ + public function redirectsToLoginIfUnauthorized() + { + $response = $this->get(route('seatgroups.index')); + + $response->assertRedirect('auth/login'); + } + + /** @test */ + public function assertUnauthorizedUser() + { + $response = $this->actingAs($this->test_user) + ->get(route('seatgroups.index')); + + $response->assertRedirect('auth/unauthorized'); + } + + /** @test */ + public function testUserWithPermissionSeesIndex() + { + + $this->giveTestUserPermission('seatgroups.view'); + $this->setMainCharacter(); + + $response = $this->actingAs($this->test_user) + ->get(route('seatgroups.index')); + + $response->assertViewIs('seatgroups::index'); + } + + /** @test */ + public function testUserSeeEditPage() + { + + $this->withoutExceptionHandling(); + $this->giveTestUserPermission('seatgroups.view'); + $this->setMainCharacter(); + + $seatgroup = factory(SeatGroup::class)->create(); + + $response = $this->actingAs($this->test_user) + ->get(route('seatgroups.edit', ['group_id' => $seatgroup->id])); + + $response->assertViewIs('seatgroups::edit'); + } + + private function giveTestUserPermission(string $permission_name) + { + // TODO: Check if AccessManager Trait could be reused + // Create a role + $role = factory(Role::class)->create(); + + // attach seatgroups.view permission to role + $permission = Permission::firstOrNew([ + 'title' => $permission_name + ]); + + $role->permissions()->save($permission); + + // give test_user role with permission + $role->groups()->save($this->group); + + $helper_collection = collect(); + + foreach ($this->group->roles as $role) { + foreach ($role->permissions as $permission) { + $helper_collection->push($permission->title); + } + } + + $this->assertTrue(in_array($permission_name, $helper_collection->toArray())); + } + + private function setMainCharacter() { - $this->assertTrue(true); + // set main_character + factory(UserSetting::class)->create([ + 'group_id' => $this->group->id, + 'name' => 'main_character_id', + 'value' => $this->test_user->id, + ]); + + $this->assertNotNull($this->group->main_character); } } diff --git a/tests/Stubs/Kernel.php b/tests/Stubs/Kernel.php new file mode 100644 index 0000000..3dfa26f --- /dev/null +++ b/tests/Stubs/Kernel.php @@ -0,0 +1,24 @@ + Authenticate::class, + 'guest' => RedirectIfAuthenticated::class, + ]; +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 7adfa3e..a77cfda 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,8 +9,9 @@ namespace Herpaderpaldent\Seat\SeatGroups\Test; use Herpaderpaldent\Seat\SeatGroups\GroupsServiceProvider; +use Herpaderpaldent\Seat\SeatGroups\Test\Stubs\Kernel; use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Route; use Orchestra\Testbench\TestCase as OrchestraTestCase; use Seat\Eveapi\Models\Character\CharacterInfo; use Seat\Eveapi\Models\Corporation\CorporationInfo; @@ -38,6 +39,7 @@ abstract class TestCase extends OrchestraTestCase protected function setUp() { parent::setUp(); + Route::auth(); // setup database $this->setupDatabase($this->app); @@ -65,8 +67,23 @@ protected function setUp() factory(User::class, 2)->create([ 'group_id' => $this->test_user->group_id ]); + + // PHP Unit Fix for Laravel packages test + if (!defined('LARAVEL_START')) { + define('LARAVEL_START', microtime(true)); + } } + /** + * Resolve application HTTP Kernel implementation. + * + * @param \Illuminate\Foundation\Application $app + * @return void + */ + protected function resolveApplicationHttpKernel($app) + { + $app->singleton('Illuminate\Contracts\Http\Kernel', Kernel::class); + } /** * Get application providers. @@ -109,6 +126,8 @@ protected function getEnvironmentSetUp($app) 'database' => ':memory:', 'prefix' => '', ]); + + //$app['router']->aliasMiddleware('auth', Authenticate::class); } } \ No newline at end of file diff --git a/tests/database/migrations/2018_05_27_085107_create_esi_statuses_table.php b/tests/database/migrations/2018_05_27_085107_create_esi_statuses_table.php new file mode 100644 index 0000000..3bda412 --- /dev/null +++ b/tests/database/migrations/2018_05_27_085107_create_esi_statuses_table.php @@ -0,0 +1,34 @@ +float('request_time'); + $table->string('status'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + + Schema::dropIfExists('esi_statuses'); + } +} \ No newline at end of file