Merge branch 'master' into 'production'

Master

See merge request Romulus21/portal!56
This commit is contained in:
Romain Delanoë
2020-07-25 11:22:23 +00:00
20 changed files with 869 additions and 11 deletions

46
.env.testing Normal file
View File

@@ -0,0 +1,46 @@
APP_NAME=Laravel
APP_ENV=test
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost:8000
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=portal
DB_USERNAME=root
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=127.0.0.1
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=no-reply@portal.loc
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

View File

@@ -10,7 +10,7 @@ services:
- mysql:latest
variables:
MYSQL_DATABASE: project_name
MYSQL_DATABASE: portal
MYSQL_ROOT_PASSWORD: secret
# This folder is cached between builds
@@ -28,11 +28,13 @@ before_script:
# Prep for Node
- apt-get install gnupg -yqq
# Upgrade to Node 8
- curl -sL https://deb.nodesource.com/setup_8.x | bash -
- curl -sL https://deb.nodesource.com/setup_10.x | bash -
# Install dependencies
- apt-get install git nodejs libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev -yqq
#- apt-get install php-mbstring php-curl php-json php-intl php-gd php-xml php-zip php-bz2 -yqq
# Install php extensions
- docker-php-ext-install mbstring pdo_mysql curl json intl gd xml zip bz2 opcache
- docker-php-ext-install pdo pdo_mysql tokenizer xml pcntl curl json
# - docker-php-ext-install mbstring intl gd xml bz2 opcache pdo_mysql curl json zip
# Install & enable Xdebug for code coverage reports
- pecl install xdebug
- docker-php-ext-enable xdebug
@@ -56,7 +58,8 @@ before_script:
- npm run dev
# Generate an application key. Re-cache.
- php artisan key:generate
- php artisan config:cache
# - php artisan config:cache
- php artisan optimize
# Run database migrations.
- php artisan migrate
# Run database seed
@@ -65,9 +68,9 @@ before_script:
test:
script:
# run laravel tests
- php vendor/bin/phpunit --coverage-text --colors=never
- php artisan test
# run frontend tests
# if you have any task for testing frontend
# set it in your package.json script
# comment this out if you don't have a frontend test
- npm test
# - npm test

View File

@@ -0,0 +1,85 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\EventCategoryRequest;
use App\Http\Resources\EventCategory as EventCategoryResource;
use App\Models\EventCategory;
use Illuminate\Http\Request;
class EventCategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
return response()->json([
'data' => EventCategoryResource::collection(EventCategory::orderBy('name')->get()),
]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(EventCategoryRequest $request)
{
$validated = $request->validated();
$category = EventCategory::create($validated);
$category->save();
return (new EventCategoryResource($category))
->response()
->setStatusCode(201);
}
/**
* Display the specified resource.
*
* @param \App\Models\EventCategory $category
* @return \Illuminate\Http\JsonResponse
*/
public function show(EventCategory $category)
{
return (new EventCategoryResource($category))
->response()
->setStatusCode(200);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\EventCategory $category
* @return \Illuminate\Http\JsonResponse
*/
public function update(EventCategoryRequest $request, EventCategory $category)
{
$category->update($request->validated());
return (new EventCategoryResource($category))
->response()
->setStatusCode(200);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\EventCategory $category
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(EventCategory $category)
{
if(auth()->user()->isAdmin()) {
$category->delete();
return response()->json([], 204);
} else {
return response()->json([], 403);
}
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\EventRequest;
use App\Models\Event;
use App\Http\Resources\Event as EventResource;
use Illuminate\Http\Request;
class EventController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param EventRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(EventRequest $request)
{
$validated = $request->validated();
$event = $request->user()->events()->create($validated);
$event->save();
return (new EventResource($event))
->response()
->setStatusCode(201);
}
/**
* Display the specified resource.
*
* @param \App\Models\Event $event
* @return \Illuminate\Http\JsonResponse
*/
public function show(Event $event)
{
return (new EventResource($event))
->response()
->setStatusCode(200);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Event $event
* @return \Illuminate\Http\Response
*/
public function edit(Event $event)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Event $event
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Event $event)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Event $event
* @return \Illuminate\Http\Response
*/
public function destroy(Event $event)
{
//
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class EventCategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->user()->isAdmin();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'description' => 'nullable|string',
];
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class EventRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return !!(auth()->user());
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'description' => 'nullable|string',
'category_id' => 'required|exists:event_categories,id',
'start_date' => 'required|date',
'end_date' => 'date|after_or_equal:start_date',
'location' => 'string|nullable'
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'name' => 'nom',
'start_date' => 'date de début',
'end_date' => 'date de fin',
'location' => 'lieu',
];
}
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'name.required' => 'A :attribute is required',
'start_date.required' => 'A :attribute is required',
];
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class Event extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'type' => 'events',
'event_id' => $this->id,
'attributes' => [
'data' => [
'name' => $this->name,
'description' => $this->description,
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'location' => $this->location,
'category' => [
'data' => [
'category_id' => $this->category_id
],
],
]
],
],
'links' => [
'self' => url('/events/'.$this->id),
]
];
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class EventCategory extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'type' => 'event categories',
'event_category_id' => $this->id,
'attributes' => [
'data' => [
'name' => $this->name,
'description' => $this->description,
]
],
],
'links' => [
'self' => url('/events/categories/'.$this->id),
]
];
}
}

10
app/Models/Event.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
protected $guarded = [];
}

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class EventCategory extends Model
{
protected $guarded = [];
}

View File

@@ -2,6 +2,7 @@
namespace App;
use App\Models\Event;
use App\Models\Image;
use App\Models\Memo;
use App\Models\ToDoList;
@@ -98,4 +99,9 @@ class User extends Authenticatable
$userImage->path = 'images/default-cover.jpg';
});
}
public function events(): HasMany
{
return $this->hasMany(Event::class);
}
}

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateEventCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('event_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('event_categories');
}
}

View File

@@ -0,0 +1,54 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateEventsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('events', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id');
$table->string('name');
$table->text('description')->nullable();
$table->timestamp('start_date');
$table->timestamp('end_date')->nullable();
$table->string('location')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('restrict')
->onUpdate('restrict');
$table->foreign('category_id')
->references('id')
->on('event_categories')
->onDelete('restrict')
->onUpdate('restrict');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('events', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['category_id']);
});
Schema::dropIfExists('events');
}
}

View File

@@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(EventCategorySeeder::class);
}
}

View File

@@ -0,0 +1,22 @@
<?php
use Illuminate\Database\Seeder;
class EventCategorySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$data = [
'name' => 'Non-classée',
'description' => 'Evénement sans catégorie',
];
$category = \App\Models\EventCategory::firstOrCreate($data);
$category->save();
}
}

View File

@@ -31,7 +31,9 @@
pre,
code {
@apply bg-gray-700 text-white p-1;
@apply text-white p-1;
background-color: #4A5568;
//color: #ffffff;
}
pre {

View File

@@ -26,6 +26,8 @@ Route::middleware('auth:api')->group(function () {
'/meteo' => 'MeteoController',
'/to-do-lists' => 'ToDoListController',
'/to-do-lists/{toDoList}/to-do' => 'ToDoController',
'/events/categories' => 'EventCategoryController',
'/events' => 'EventController',
// '/users/{user}/posts' => 'UserPostController',
// '/friend-request' => 'FriendRequestController',
]);

View File

@@ -0,0 +1,321 @@
<?php
namespace Tests\Feature;
use App\Models\Event;
use App\Models\EventCategory;
use App\Models\Memo;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Symfony\Component\HttpFoundation\Response;
use Tests\TestCase;
class EventsTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function an_admin_can_create_an_event_category()
{
$this->withoutExceptionHandling();
$this->actingAs($user = factory(User::class)->create(['role' => 2]), 'api');
$response = $this->post('/api/events/categories', [
'name' => 'Test name event category',
'description' => 'Test description event category',
])->assertStatus(201);
$category = EventCategory::first();
$this->assertEquals('Test name event category', $category->name);
$this->assertEquals('Test description event category', $category->description);
$response->assertJson([
'data' => [
'type' => 'event categories',
'event_category_id' => $category->id,
'attributes' => [
'data' => [
'name' => $category->name,
'description' => $category->description,
]
],
],
'links' => [
'self' => url('/events/categories/'.$category->id),
]
]);
}
/** @test */
public function event_category_name_are_required()
{
$this->actingAs($user = factory(\App\User::class)->create(['role' => 2]), 'api');
$response = $this->post('/api/events/categories', ['name' => '', 'description' => 'test name required']);
$response->assertSessionHasErrors('name');
$this->assertCount(0, EventCategory::all());
}
/** @test */
public function only_admin_can_create_event_category()
{
$this->actingAs($user = factory(User::class)->create(), 'api');
$response = $this->post('/api/events/categories', ['name' => 'Test fail', 'description' => 'test name required']);
$response->assertStatus(403);
$this->assertCount(0, EventCategory::all());
}
/** @test */
public function an_event_category_can_be_retrieved()
{
$this->actingAs($user = factory(User::class)->create(), 'api');
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
$response = $this->get('/api/events/categories/' . $category->id );
$response->assertJson([
'data' => [
'type' => 'event categories',
'event_category_id' => $category->id,
'attributes' => [
'data' => [
'name' => $category->name,
'description' => $category->description,
]
],
]
]);
}
/** @test */
public function all_event_categories_can_be_retrieved()
{
$this->actingAs($user = factory(User::class)->create(), 'api');
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
$categoryTwo = EventCategory::create(['name' => 'Test category 2', 'description' => 'Test description']);
$categoryTree = EventCategory::create(['name' => 'Test category 3', 'description' => 'Test description']);
$response = $this->get('/api/events/categories');
$response->assertJson([
'data' => [
[
'data' => [
'type' => 'event categories',
'event_category_id' => $category->id,
'attributes' => [
'data' => [
'name' => $category->name,
'description' => $category->description,
]
],
],
],
[
'data' => [
'type' => 'event categories',
'event_category_id' => $categoryTwo->id,
'attributes' => [
'data' => [
'name' => $categoryTwo->name,
'description' => $categoryTwo->description,
]
],
],
],
[
'data' => [
'type' => 'event categories',
'event_category_id' => $categoryTree->id,
'attributes' => [
'data' => [
'name' => $categoryTree->name,
'description' => $categoryTree->description,
]
],
],
],
],
]);
}
/** @test */
public function a_event_category_can_be_patch()
{
$this->actingAs($user = factory(User::class)->create(['role' => 2]), 'api');
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
$response = $this->patch('/api/events/categories/' . $category->id, ['name' => 'Update Catégory']);
$category = $category->fresh();
$this->assertEquals('Update Catégory', $category->name);
$response->assertStatus(200);
$response->assertJson([
'data' => [
'type' => 'event categories',
'event_category_id' => $category->id,
'attributes' => [
'data' => [
'name' => $category->name,
'description' => $category->description,
]
],
],
]);
}
/** @test */
public function only_the_admin_can_patch_the_memo()
{
$this->actingAs($user = factory(User::class)->create(), 'api');
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
$response = $this->patch('/api/events/categories/' . $category->id, ['name' => 'try to update']);
$response->assertStatus(403);
}
/** @test */
public function an_event_category_can_be_delete()
{
$this->actingAs($user = factory(User::class)->create(['role' => 2]), 'api');
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
$response = $this->delete('/api/events/categories/' . $category->id);
$category = $category->fresh();
$this->assertCount(0, EventCategory::all());
$response->assertStatus(204);
}
/** @test */
public function only_admin_can_delete_an_event_category()
{
$this->actingAs($user = factory(User::class)->create(), 'api');
$category = EventCategory::create(['name' => 'Test category', 'description' => 'Test description']);
$response = $this->delete('/api/events/categories/' . $category->id);
$response->assertStatus(403);
}
/** @test */
public function a_user_can_create_an_event()
{
$this->withoutExceptionHandling();
$this->actingAs($user = factory(User::class)->create(), 'api');
app(\DatabaseSeeder::class)->call(\EventCategorySeeder::class);
//dd(EventCategory::all());
$response = $this->post('/api/events', $this->data())->assertStatus(201);
$event = Event::first();
$this->assertEquals($user->id, $event->user_id);
$this->assertEquals(1, $event->category_id);
$this->assertEquals('Test name event', $event->name);
$this->assertEquals('Test description event', $event->description);
$this->assertEquals('2020-07-20 09:00:00', $event->start_date);
$this->assertEquals('2020-07-26 09:00:00', $event->end_date);
$this->assertEquals('Marcillac', $event->location);
$response->assertJson([
'data' => [
'type' => 'events',
'event_id' => $event->id,
'attributes' => [
'data' => [
'name' => $event->name,
'description' => $event->description,
'start_date' => $event->start_date,
'end_date' => $event->end_date,
'location' => $event->location,
'category' => [
'data' => [
'category_id' => 1
],
],
]
],
],
'links' => [
'self' => url('/events/'.$event->id),
]
]);
}
/** @test */
public function event_name_are_required()
{
$this->actingAs($user = factory(\App\User::class)->create(), 'api');
$response = $this->post('/api/events', array_merge($this->data(), ['name' => '']));
$response->assertSessionHasErrors('name');
$this->assertCount(0, EventCategory::all());
}
/** @test */
public function event_start_date_are_required()
{
$this->actingAs($user = factory(\App\User::class)->create(), 'api');
$response = $this->post('/api/events', array_merge($this->data(), ['start_date' => '']));
$response->assertSessionHasErrors('start_date');
$this->assertCount(0, EventCategory::all());
}
/** @test */
public function an_event_can_be_retrieved()
{
$this->actingAs($user = factory(User::class)->create(), 'api');
app(\DatabaseSeeder::class)->call(\EventCategorySeeder::class);
$event = $user->events()->create($this->data());
$response = $this->get('/api/events/' . $event->id );
$response->assertJson([
'data' => [
'type' => 'events',
'event_id' => $event->id,
'attributes' => [
'data' => [
'name' => $event->name,
'description' => $event->description,
'start_date' => $event->start_date,
'end_date' => $event->end_date,
'location' => $event->location,
'category' => [
'data' => [
'category_id' => 1
],
],
]
],
],
]);
}
private function data()
{
return [
'name' => 'Test name event',
'description' => 'Test description event',
'category_id' => 1,
'start_date' => '2020-07-20 09:00:00',
'end_date' => '2020-07-26 09:00:00',
'location' => 'Marcillac',
];
}
}

View File

@@ -99,7 +99,7 @@ class ImagesTest extends TestCase
'profile_image' => [
'data' => [
'type' => 'images',
'image_id' => 2,
'image_id' => 3,
'attributes' => []
]
]

View File

@@ -14,14 +14,14 @@ class MemosTest extends TestCase
use RefreshDatabase;
/** @test */
public function an_unauthenticated_user_should_redirect_to_login()
/* public function an_unauthenticated_user_should_redirect_to_login()
{
$response = $this->post('/api/memos', $this->data());
$this->assertGuest($guard = null);
$response->assertRedirect('/login');
$this->assertCount(0, Memo::all());
}
} */
/** @test */
public function an_unauthenticated_user_can_add_a_memo()