From 4317178914c5c7817dc914f37e3d47eea547ae10 Mon Sep 17 00:00:00 2001 From: Andy Pfister Date: Wed, 9 Jan 2019 10:23:42 +0100 Subject: [PATCH] Renamed foreign keys [#150] --- api/app/Holiday.php | 11 +-- api/app/HolidayType.php | 2 + .../Http/Controllers/api/AuthController.php | 6 +- .../Controllers/api/FeedbackController.php | 2 +- .../Controllers/api/HolidayController.php | 2 +- .../Controllers/api/MissionController.php | 16 ++-- .../Http/Controllers/api/PDFController.php | 3 +- .../Controllers/api/PaymentController.php | 14 +-- .../Controllers/api/ReportSheetController.php | 7 +- .../Http/Controllers/api/UserController.php | 6 +- api/app/Mission.php | 14 +-- api/app/PaymentEntry.php | 23 +++-- api/app/ReportSheet.php | 75 ++++++---------- api/app/Services/PDF/AufgebotPDF.php | 8 +- api/app/Services/PDF/PhoneListPDF.php | 4 +- api/app/Services/PDF/SpesenStatistik.php | 2 +- api/app/Services/PDF/ZiviReportSheetPDF.php | 2 +- api/app/User.php | 46 ++++------ api/app/UserFeedback.php | 28 +----- api/database/factories/HolidayFactory.php | 6 +- api/database/factories/HolidayTypeFactory.php | 11 +++ api/database/factories/MissionFactory.php | 8 +- .../factories/PaymentEntryFactory.php | 21 +++++ api/database/factories/ReportsheetFactory.php | 4 +- api/database/factories/UserFactory.php | 12 +-- .../factories/UserFeedbackFactory.php | 4 +- .../2019_01_08_154346_rename_foreign_keys.php | 82 ++++++++++++++++++ api/tests/TestCase.php | 4 +- api/tests/Unit/HolidayTest.php | 17 ++++ api/tests/integrations/AuthControllerTest.php | 2 +- .../integrations/MissionControllerTest.php | 33 +++++-- api/tests/integrations/PDFControllerTest.php | 11 +-- .../integrations/PaymentControllerTest.php | 86 ++++++++++++++++++- .../ReportSheetControllerTest.php | 12 ++- api/tests/integrations/UserControllerTest.php | 2 +- 35 files changed, 397 insertions(+), 189 deletions(-) create mode 100644 api/database/factories/HolidayTypeFactory.php create mode 100644 api/database/factories/PaymentEntryFactory.php create mode 100644 api/database/migrations/2019_01_08_154346_rename_foreign_keys.php create mode 100644 api/tests/Unit/HolidayTest.php diff --git a/api/app/Holiday.php b/api/app/Holiday.php index 57ecc122..d4af7b24 100755 --- a/api/app/Holiday.php +++ b/api/app/Holiday.php @@ -6,15 +6,10 @@ class Holiday extends Model { - protected $fillable = ['id', - 'date_from', - 'date_to', - 'holiday_type', - 'description' - ]; + protected $fillable = ['date_from', 'date_to', 'description', 'holiday_type_id' ]; - public function holidayType() + public function holiday_type() { - return $this->belongsTo('App\HolidayType', 'holiday_type'); + return $this->belongsTo(HolidayType::class); } } diff --git a/api/app/HolidayType.php b/api/app/HolidayType.php index ce93b767..bcdb7b27 100755 --- a/api/app/HolidayType.php +++ b/api/app/HolidayType.php @@ -14,4 +14,6 @@ public function holiday() { return $this->hasMany('App\Holiday'); } + + public $timestamps = false; } diff --git a/api/app/Http/Controllers/api/AuthController.php b/api/app/Http/Controllers/api/AuthController.php index 6357b949..dea44874 100755 --- a/api/app/Http/Controllers/api/AuthController.php +++ b/api/app/Http/Controllers/api/AuthController.php @@ -34,7 +34,7 @@ protected function jwt(User $user) $payload = [ 'iss' => "izivi-api", // Issuer of the token 'sub' => $user->id, // Subject of the token - 'isAdmin' => $user->role == 1, + 'isAdmin' => $user->isAdmin(), 'iat' => time(), // Time when JWT was issued. 'exp' => time() + 60*60*24, // Expiration time, ]; @@ -147,8 +147,8 @@ public function postRegister(Request $request) $user->phone_business = ""; $user->phone_mobile = ""; $user->phone_private = ""; - $user->regional_center = 1; - $user->role = AuthController::USER_ROLE_ZIVI; + $user->regional_center_id = 1; + $user->role_id = AuthController::USER_ROLE_ZIVI; $user->zdp = $request->input("zdp"); $user->save(); diff --git a/api/app/Http/Controllers/api/FeedbackController.php b/api/app/Http/Controllers/api/FeedbackController.php index aa606c4d..c5a4fa21 100644 --- a/api/app/Http/Controllers/api/FeedbackController.php +++ b/api/app/Http/Controllers/api/FeedbackController.php @@ -48,7 +48,7 @@ public function postFeedback() foreach ($content['answers'] as $answer) { $user_feedback = new UserFeedback(); - $user_feedback->user = $userId; + $user_feedback->user_id = $userId; $user_feedback->feedbackId = $feedbackId; $user_feedback->year = $date; $user_feedback->questionId = $answer['id']; diff --git a/api/app/Http/Controllers/api/HolidayController.php b/api/app/Http/Controllers/api/HolidayController.php index d056cb42..5a0bc428 100644 --- a/api/app/Http/Controllers/api/HolidayController.php +++ b/api/app/Http/Controllers/api/HolidayController.php @@ -41,7 +41,7 @@ private function validateRequest(Request $request) 'date_from' => 'required|date', 'date_to' => 'required|date', 'description' => 'required|string', - 'holiday_type' => 'required|integer' + 'holiday_type_id' => 'required|integer' ]); } } diff --git a/api/app/Http/Controllers/api/MissionController.php b/api/app/Http/Controllers/api/MissionController.php index 5e350a37..3549a5a4 100644 --- a/api/app/Http/Controllers/api/MissionController.php +++ b/api/app/Http/Controllers/api/MissionController.php @@ -23,8 +23,8 @@ public function delete($id) public function indexByYear($year) { // TODO remove join and work with Laravel relations instead - $data = Mission::join('users', 'users.id', '=', 'missions.user') - ->join('specifications', 'specifications.id', '=', 'missions.specification') + $data = Mission::join('users', 'users.id', '=', 'missions.user_id') + ->join('specifications', 'specifications.id', '=', 'missions.specification_id') ->select('*', 'users.id AS userid') ->whereNull('missions.deleted_at') ->whereDate('end', '>=', $year . '-01-01') @@ -51,7 +51,7 @@ public function post(Request $request) { $validatedData = $this->validateRequest($request); - if (Auth::user()->isAdmin() || Auth::id() == $validatedData['user']) { + if (Auth::user()->isAdmin() || Auth::id() == $validatedData['user_id']) { $mission = new Mission($validatedData); $mission->feedback_mail_sent = false; $mission->feedback_done = false; @@ -63,8 +63,8 @@ public function post(Request $request) // TODO replace this piece as soon as the frontend implementation of the Profile view is specified $user = Auth::user(); - if ($mission->user == 'me') { - $mission->user = $user->id; + if ($mission->user_id == 'me') { + $mission->user_id = $user->id; } $mission->save(); @@ -78,7 +78,7 @@ public function put($id, Request $request) { $mission = Mission::findOrFail($id); - if (Auth::user()->isAdmin() || Auth::id() == $mission->user) { + if (Auth::user()->isAdmin() || Auth::id() == $mission->user_id) { DB::beginTransaction(); try { $validatedData = $this->validateRequest($request); @@ -167,9 +167,9 @@ private function validateRequest(Request $request) 'long_mission' => 'required|boolean', 'mission_type' => 'required|integer', 'probation_period' => 'required|boolean', - 'specification' => 'required|integer', + 'specification_id' => 'required|integer', 'start' => 'required|date', - 'user' => 'required|integer' + 'user_id' => 'required|integer' ]); } } diff --git a/api/app/Http/Controllers/api/PDFController.php b/api/app/Http/Controllers/api/PDFController.php index 8e85eed2..8ebae30d 100644 --- a/api/app/Http/Controllers/api/PDFController.php +++ b/api/app/Http/Controllers/api/PDFController.php @@ -85,11 +85,10 @@ public function getAufgebot(Application $app, $id) //Allow only admins to get reportSheets of other Users $user = Auth::user(); - if ($user->role!=1 && $user->id!=$aufgebot->getUserId()) { + if (!$user->isAdmin() && $user->id!=$aufgebot->getUserId()) { return response("unauthorized", 401); } - $response = response()->download($aufgebot->createPDF(), 'aufgebot.pdf') ->deleteFileAfterSend(true); $response->headers->set("Content-Type", "application/pdf"); diff --git a/api/app/Http/Controllers/api/PaymentController.php b/api/app/Http/Controllers/api/PaymentController.php index b002888c..0327d10c 100644 --- a/api/app/Http/Controllers/api/PaymentController.php +++ b/api/app/Http/Controllers/api/PaymentController.php @@ -180,11 +180,11 @@ public function getIsoPaymentXml() $sheet->save(); $paymentEntry = new PaymentEntry(); - $paymentEntry->payment = $payment->id; + $paymentEntry->payment_id = $payment->id; $paymentEntry->amount = $element['amount']*100; - $paymentEntry->user = $element['userid']; + $paymentEntry->user_id = $element['userid']; $paymentEntry->iban = $element['iban']; - $paymentEntry->report_sheet = $element['sheet_id']; + $paymentEntry->report_sheet_id = $element['sheet_id']; $paymentEntry->save(); } @@ -199,10 +199,10 @@ public function getArchivedPayment($id) { $payment = Payment::find($id); $payment->sheets = PaymentEntry:: - join('users', 'users.id', '=', 'payment_entries.user') - ->join('report_sheets', 'report_sheets.id', '=', 'payment_entries.report_sheet') - ->select('amount', 'report_sheet', 'first_name', 'last_name', 'zdp', 'users.id AS userid', 'iban', 'state') - ->where('payment', '=', $id)->get(); + join('users', 'users.id', '=', 'payment_entries.user_id') + ->join('report_sheets', 'report_sheets.id', '=', 'payment_entries.report_sheet_id') + ->select('amount', 'report_sheet_id', 'first_name', 'last_name', 'zdp', 'users.id AS userid', 'iban', 'state') + ->where('payment_id', '=', $id)->get(); return new JsonResponse($payment); } diff --git a/api/app/Http/Controllers/api/ReportSheetController.php b/api/app/Http/Controllers/api/ReportSheetController.php index d196a268..bf63fb99 100644 --- a/api/app/Http/Controllers/api/ReportSheetController.php +++ b/api/app/Http/Controllers/api/ReportSheetController.php @@ -24,7 +24,7 @@ public function index() { if (!Auth::user()->isAdmin()) { // TODO Improve this piece by using Auth::user->report_sheets, then use the Laravel Collection where and select functions - $reportSheets = ReportSheet::join('users', 'report_sheets.user', '=', 'users.id') + $reportSheets = ReportSheet::join('users', 'report_sheets.user_id', '=', 'users.id') ->select('report_sheets.id AS id', 'start', 'end', 'state') ->where('users.id', '=', Auth::id()) ->where('state', '>', '0') @@ -37,9 +37,10 @@ public function index() return $reportSheets; } else { - return ReportSheet::with('user')->orderBy('start', 'desc') + return ReportSheet::join('users', 'users.id', '=', 'report_sheets.user_id') + ->orderBy('start', 'desc') ->orderBy('end', 'desc') - ->orderBy('zdp') + ->orderBy('users.zdp') ->get(); } } diff --git a/api/app/Http/Controllers/api/UserController.php b/api/app/Http/Controllers/api/UserController.php index b53a2c92..43053c3a 100755 --- a/api/app/Http/Controllers/api/UserController.php +++ b/api/app/Http/Controllers/api/UserController.php @@ -74,7 +74,7 @@ public function get($id) public function index() { - return User::with(['user_role', 'missions'])->get(); + return User::with(['role', 'missions'])->get(); } public function put($id, Request $request) @@ -110,7 +110,7 @@ private function basicUserValidationRules() 'hometown' => 'required|string', 'last_name' => 'required|string', 'phone' => 'required|string', - 'regional_center' => 'required|integer', + 'regional_center_id' => 'required|integer', 'work_experience' => 'required|string', 'zip' => 'required|integer', ]; @@ -120,7 +120,7 @@ private function extendedUserValidationRules() { return [ 'internal_note' => 'string', - 'role' => 'required|integer', + 'role_id' => 'required|integer', ]; } } diff --git a/api/app/Mission.php b/api/app/Mission.php index d16bb4ff..bea0805c 100755 --- a/api/app/Mission.php +++ b/api/app/Mission.php @@ -2,7 +2,6 @@ namespace App; -use App\ReportSheet; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; @@ -11,8 +10,8 @@ class Mission extends Model use SoftDeletes; protected $fillable = ['id', - 'user', - 'specification', // "Pflichtenheft" + 'user_id', + 'specification_id', // "Pflichtenheft" 'start', 'end', 'draft', // "Aufgebot" @@ -31,9 +30,14 @@ class Mission extends Model 'first_time' => 'boolean' ]; - public function usermodel() + public function specification() { - return $this->belongsTo('App\User', 'user'); + return $this->belongsTo(Specification::class); + } + + public function user() + { + return $this->belongsTo(User::class); } public function getFormattedDate($fieldName) diff --git a/api/app/PaymentEntry.php b/api/app/PaymentEntry.php index 9a9cf12d..be57ec4f 100755 --- a/api/app/PaymentEntry.php +++ b/api/app/PaymentEntry.php @@ -6,10 +6,21 @@ class PaymentEntry extends Model { - protected $fillable = ['id', - 'user', - 'report_sheet', - 'amount', - 'iban' - ]; + protected $fillable = ['amount','iban', 'id', 'payment_id', 'report_sheet_id', 'user_id']; + + public function report_sheet() + { + return $this->belongsTo(ReportSheet::class); + } + + public function payment() + { + return $this->belongsTo(Payment::class); + } + + // TODO drop relation with user + public function user() + { + return $this->belongsTo(User::class); + } } diff --git a/api/app/ReportSheet.php b/api/app/ReportSheet.php index cd840289..0482fc80 100755 --- a/api/app/ReportSheet.php +++ b/api/app/ReportSheet.php @@ -13,50 +13,29 @@ class ReportSheet extends Model { use SoftDeletes; - protected $fillable = ['id', - 'start', - 'end', - 'user', - 'work', - 'work_comment', - 'national_holiday', - 'company_holiday', - 'company_holiday_comment', - 'workfree', - 'workfree_comment', - 'additional_workfree', - 'additional_workfree_comment', - 'ill', - 'ill_comment', - 'holiday', - 'holiday_comment', - 'vacation', - 'vacation_comment', - 'driving_charges', - 'driving_charges_comment', - 'extraordinarily', - 'extraordinarily_comment', - 'clothes', - 'clothes_comment', - 'employmentId', - 'bank_account_number', - 'document_number', // "Beleg Nummer" - 'state', - 'ignore_first_last_day', - ]; - + protected $fillable = ['additional_workfree', 'additional_workfree_comment', 'bank_account_number', 'clothes', + 'clothes_comment', 'company_holiday', 'company_holiday_comment', 'document_number', 'driving_charges', + 'driving_charges_comment', 'end', 'extraordinarily', 'extraordinarily_comment', 'holiday', 'holiday_comment', ' + ignore_first_last_day', 'ill', 'ill_comment', 'mission_id', 'national_holiday', 'start', 'state', 'user_id', + 'vacation', 'vacation_comment', 'work', 'work_comment', 'workfree', 'workfree_comment', + ]; + + public function mission() + { + return $this->belongsTo(Mission::class); + } public function user() { - return $this->belongsTo('App\User', 'user'); + return $this->belongsTo(User::class); } // TODO replace this function with a proper get method in the controller static function getSpesen($reportSheetId) { - $reportSheet = ReportSheet::join('missions', 'missions.id', '=', 'report_sheets.mission') - ->join('specifications', 'specifications.id', '=', 'missions.specification') - ->join('users', 'users.id', '=', 'report_sheets.user') + $reportSheet = ReportSheet::join('missions', 'missions.id', '=', 'report_sheets.mission_id') + ->join('specifications', 'specifications.id', '=', 'missions.specification_id') + ->join('users', 'users.id', '=', 'report_sheets.user_id') ->where('report_sheets.id', '=', $reportSheetId) ->select( 'report_sheets.start AS meldeblaetter_start', @@ -89,7 +68,7 @@ static function getSpesen($reportSheetId) 'missions.id AS mission_id', 'missions.start AS einsaetze_start', 'missions.end AS einsaetze_end', - 'missions.specification AS einsaetze_pflichtenheft', + 'missions.specification_id AS einsaetze_pflichtenheft', 'missions.eligible_holiday AS einsaetze_eligibleholiday', 'specifications.id AS pflichtenheft_id', 'specifications.name AS pflichtenheft_name', @@ -130,18 +109,18 @@ static function getSpesen($reportSheetId) $ziviferien = $reportSheet['einsaetze_eligibleholiday']; $ziviferienbisher = ReportSheet::where('report_sheets.id', '!=', $reportSheetId) - ->where('mission', '=', $reportSheet['mission_id']) - ->groupBy('user') + ->where('mission_id', '=', $reportSheet['mission_id']) + ->groupBy('user_id') ->selectRaw('(SUM(company_holiday_holiday) + SUM(holiday)) AS ferienbisher') ->first()['ferienbisher']; - $betriebsferien = Holiday::join('holiday_types', 'holidays.holiday_type', '=', 'holiday_types.id') + $betriebsferien = Holiday::join('holiday_types', 'holidays.holiday_type_id', '=', 'holiday_types.id') ->whereDate('date_from', '<=', $reportSheet['meldeblaetter_end']) ->whereDate('date_to', '>=', $reportSheet['meldeblaetter_start']) ->where('holiday_types.name', '=', 'Betriebsferien') ->get(); - $holiday_feiertage = Holiday::join('holiday_types', 'holidays.holiday_type', '=', 'holiday_types.id') + $holiday_feiertage = Holiday::join('holiday_types', 'holidays.holiday_type_id', '=', 'holiday_types.id') ->whereDate('date_from', '<=', $reportSheet['meldeblaetter_end']) ->whereDate('date_to', '>=', $reportSheet['meldeblaetter_start']) ->where('holiday_types.name', '=', 'Feiertag') @@ -264,7 +243,7 @@ static function getSpesen($reportSheetId) $verfügbare_krankheitstage+=5; } - $krankheitstage_bisher = ReportSheet::selectRaw('SUM(`ill`) AS d')->where('mission', '=', $reportSheet['mission_id'])->first()['d']; + $krankheitstage_bisher = ReportSheet::selectRaw('SUM(`ill`) AS d')->where('mission_id', '=', $reportSheet['mission_id'])->first()['d']; $reportSheet['krankheitstage_verbleibend'] = $verfügbare_krankheitstage - $krankheitstage_bisher; @@ -276,7 +255,7 @@ static function getSpesen($reportSheetId) $reportSheet['meldeblaetter_kleider_proposal'] = min(240, $reportSheet['meldeblaetter_kleider_proposal']); $bisher = ReportSheet::selectRaw('SUM(clothes) AS s') - ->where('mission', '=', $reportSheet['mission_id']) + ->where('mission_id', '=', $reportSheet['mission_id']) ->where('start', '<', $reportSheet['meldeblaetter_start'])->first()['s'] / 100; $reportSheet['meldeblaetter_kleider_proposal'] = min($reportSheet['meldeblaetter_kleider_proposal'], 240-$bisher); @@ -463,13 +442,13 @@ private static function subtractFreeDays($start, $end, $dayCount, $long_mission) { $ziviHolidays = MissionController::calculateZiviHolidays($long_mission, $dayCount); - $betriebsferien = Holiday::join('holiday_types', 'holidays.holiday_type', '=', 'holiday_types.id') + $betriebsferien = Holiday::join('holiday_types', 'holidays.holiday_type_id', '=', 'holiday_types.id') ->whereDate('date_from', '<=', $end) ->whereDate('date_to', '>=', $start) ->where('holiday_types.name', '=', 'Betriebsferien') ->get(); - $feiertage = Holiday::join('holiday_types', 'holidays.holiday_type', '=', 'holiday_types.id') + $feiertage = Holiday::join('holiday_types', 'holidays.holiday_type_id', '=', 'holiday_types.id') ->whereDate('date_from', '<=', $end) ->whereDate('date_to', '>=', $start) ->where('holiday_types.name', '=', 'Feiertag') @@ -514,8 +493,8 @@ private static function addDaysToDate($dateString, $days) public static function add($mission, $start, $end) { $sheet = new ReportSheet(); - $sheet->mission = $mission->id; - $sheet->user = $mission->user; + $sheet->mission_id = $mission->id; + $sheet->user_id = $mission->user_id; $sheet->start = $start; $sheet->end = $end; $sheet->bank_account_number = CompanyInfo::DEFAULT_ACCOUNT_NUMBER_REPORT_SHEETS; @@ -533,7 +512,7 @@ public static function add($mission, $start, $end) public static function deleteByMission($missionId) { // TODO remove this method and solve it through database cascade instead - $reportSheets = ReportSheet::where('mission', '=', $missionId); + $reportSheets = ReportSheet::where('mission_id', '=', $missionId); $reportSheets->delete(); } } diff --git a/api/app/Services/PDF/AufgebotPDF.php b/api/app/Services/PDF/AufgebotPDF.php index 5ca03c45..f404277c 100644 --- a/api/app/Services/PDF/AufgebotPDF.php +++ b/api/app/Services/PDF/AufgebotPDF.php @@ -29,13 +29,13 @@ public function __construct($missionId) parent::__construct(); $this->einsatz = Mission::find($missionId); - $this->pflichtenheft = Specification::find($this->einsatz->specification); - $this->zivi = User::find($this->einsatz->user); - $this->regionalzentrum = RegionalCenter::find($this->zivi->regional_center); + $this->pflichtenheft = $this->einsatz->specification; + $this->zivi = $this->einsatz->user; + $this->regionalzentrum = RegionalCenter::find($this->zivi->regional_center_id); $this->companyHolidays = Holiday::whereDate("date_from", "<=", $this->einsatz->end) ->whereDate("date_to", ">=", $this->einsatz->start) - ->where("holiday_type", "=", "1") + ->where("holiday_type_id", "=", "1") ->get(); } diff --git a/api/app/Services/PDF/PhoneListPDF.php b/api/app/Services/PDF/PhoneListPDF.php index 44235802..10842f43 100644 --- a/api/app/Services/PDF/PhoneListPDF.php +++ b/api/app/Services/PDF/PhoneListPDF.php @@ -37,8 +37,8 @@ public function __construct($from, $to) $specifications = Specification::select('*', 'id')->get(); foreach ($specifications as $specification) { - $zivis = Mission::join('users', 'users.id', '=', 'missions.user') - ->where('specification', '=', $specification->id) + $zivis = Mission::join('users', 'users.id', '=', 'missions.user_id') + ->where('specification_id', '=', $specification->id) ->whereDate('start', '<=', $to) ->whereDate('end', '>=', $from) ->orderBy('last_name', 'first_name')->get(); diff --git a/api/app/Services/PDF/SpesenStatistik.php b/api/app/Services/PDF/SpesenStatistik.php index c14fe8e4..97bc3666 100644 --- a/api/app/Services/PDF/SpesenStatistik.php +++ b/api/app/Services/PDF/SpesenStatistik.php @@ -615,7 +615,7 @@ private function generateEmptyGeldOrTageArray() private function getMeldeblaetterInPeriod($start_TS, $end_TS) { - $query = ReportSheet::join('users', 'users.id', '=', 'report_sheets.user'); + $query = ReportSheet::join('users', 'users.id', '=', 'report_sheets.user_id'); if ($this->showOnlyDoneSheets) { $query = $query->where('report_sheets.state', '=', '3'); } diff --git a/api/app/Services/PDF/ZiviReportSheetPDF.php b/api/app/Services/PDF/ZiviReportSheetPDF.php index 9deb0904..e5535625 100644 --- a/api/app/Services/PDF/ZiviReportSheetPDF.php +++ b/api/app/Services/PDF/ZiviReportSheetPDF.php @@ -39,7 +39,7 @@ public function __construct($spesenId) $this->spesen = ReportSheet::getSpesen($spesenId); - $this->user = ReportSheet::find($spesenId)->user()->first(); + $this->user = ReportSheet::find($spesenId)->user; } public function getUserId() diff --git a/api/app/User.php b/api/app/User.php index 43db1372..14ef72a6 100755 --- a/api/app/User.php +++ b/api/app/User.php @@ -22,30 +22,9 @@ class User extends Model implements * * @var array */ - protected $fillable = [ - 'email', - 'zdp', - 'first_name', - 'last_name', - 'email', - 'role', - 'address', - 'zip', - 'city', - 'birthday', - 'hometown', - 'phone_mobile', - 'phone_private', - 'phone_business', - 'bank_iban', - 'bank_bic', - 'work_experience', - 'driving_licence', - 'ga_travelcard', - 'half_fare_travelcard', - 'other_fare_network', - 'regional_center', - 'internal_note' + protected $fillable = ['address', 'bank_bic', 'bank_iban', 'birthday', 'chainsaw_workshop', 'city', + 'driving_licence_b', 'driving_licence_be', 'email', 'first_name', 'internal_note', 'hometown', 'last_name', + 'phone', 'regional_center_id', 'role_id', 'work_experience', 'zdp', 'zip' ]; /** @@ -66,19 +45,24 @@ class User extends Model implements // TODO Check phone number formatting in update hook - public function user_role() + public function missions() { - return $this->belongsTo(Role::class, 'role'); + return $this->hasMany(Mission::class); } - public function missions() + public function regional_center() { - return $this->hasMany('App\Mission', 'user'); + return $this->belongsTo(RegionalCenter::class); } public function report_sheets() { - return $this->hasMany('App\ReportSheet', 'user'); + return $this->hasMany(ReportSheet::class); + } + + public function role() + { + return $this->belongsTo(Role::class); } /** @@ -99,7 +83,7 @@ public function getJWTIdentifier() public function getJWTCustomClaims() { return [ - 'isAdmin' => $this->role==1 + 'isAdmin' => $this->role_id == 1 ]; } @@ -111,7 +95,7 @@ public function getJWTCustomClaims() public function isAdmin() { $role_admin = Role::where('name', '=', 'admin')->first(); - if ($this->role === $role_admin['id']) { + if ($this->role_id === $role_admin['id']) { return true; } else { return false; diff --git a/api/app/UserFeedback.php b/api/app/UserFeedback.php index 6596f514..c218c24c 100755 --- a/api/app/UserFeedback.php +++ b/api/app/UserFeedback.php @@ -8,32 +8,10 @@ class UserFeedback extends Model { public $timestamps = false; - protected $fillable = [ - 'id', - 'user', - 'year', - 'questionId', - 'answer' + protected $fillable = ['answer', 'questionId', 'user_id', 'year' ]; - ]; - - /** - * Get the identifier that will be stored in the subject claim of the JWT. - * - * @return mixed - */ - public function getJWTIdentifier() - { - return $this->getKey(); - } - - /** - * Return a key value array, containing any custom claims to be added to the JWT. - * - * @return array - */ - public function getJWTCustomClaims() + public function user() { - return []; + return $this->belongsTo(User::class); } } diff --git a/api/database/factories/HolidayFactory.php b/api/database/factories/HolidayFactory.php index 0ca18369..54b98fba 100644 --- a/api/database/factories/HolidayFactory.php +++ b/api/database/factories/HolidayFactory.php @@ -8,7 +8,9 @@ return [ 'date_from' => $faker->dateTimeBetween('+0 days', '+2 years')->format('Y-m-d'), 'date_to' => $faker->dateTimeBetween('+0 days', '+2 years')->format('Y-m-d'), - 'holiday_type' => $faker->numberBetween(1, 2), - 'description' => $faker->sentence + 'description' => $faker->sentence, + 'holiday_type_id' => function () { + return factory(\App\HolidayType::class)->create()->id; + }, ]; }); diff --git a/api/database/factories/HolidayTypeFactory.php b/api/database/factories/HolidayTypeFactory.php new file mode 100644 index 00000000..5946a48f --- /dev/null +++ b/api/database/factories/HolidayTypeFactory.php @@ -0,0 +1,11 @@ +define(App\HolidayType::class, function (Generator $faker) { + return [ + 'name' => $faker->word + ]; +}); diff --git a/api/database/factories/MissionFactory.php b/api/database/factories/MissionFactory.php index 3f0eef6b..b3509f47 100644 --- a/api/database/factories/MissionFactory.php +++ b/api/database/factories/MissionFactory.php @@ -15,8 +15,12 @@ 'long_mission' => $faker->boolean(), 'mission_type' => $faker->numberBetween(0, 2), 'probation_period' => $faker->numberBetween(0, 10), - 'specification' => factory(App\Specification::class)->create()->id, + 'specification_id' => function () { + return factory(App\Specification::class)->create()->id; + }, 'start' => $faker->dateTimeBetween('-180 days', '-90 days'), - 'user' => factory(App\User::class)->create()->id + 'user_id' => function () { + return factory(\App\User::class)->create()->id; + } ]; }); diff --git a/api/database/factories/PaymentEntryFactory.php b/api/database/factories/PaymentEntryFactory.php new file mode 100644 index 00000000..48ecffc2 --- /dev/null +++ b/api/database/factories/PaymentEntryFactory.php @@ -0,0 +1,21 @@ +define(App\PaymentEntry::class, function (Generator $faker) { + return [ + 'amount' => $faker->numberBetween(1000, 100000), + 'iban' => $faker->iban('CH'), + 'payment_id' => function () { + return factory(\App\Payment::class)->create()->id; + }, + 'report_sheet_id' => function () { + return factory(\App\ReportSheet::class)->create()->id; + }, + 'user_id' => function () { + return factory(\App\User::class)->create()->id; + } + ]; +}); diff --git a/api/database/factories/ReportsheetFactory.php b/api/database/factories/ReportsheetFactory.php index 6b01e2d1..74ea37b4 100644 --- a/api/database/factories/ReportsheetFactory.php +++ b/api/database/factories/ReportsheetFactory.php @@ -21,12 +21,12 @@ 'ignore_first_last_day' => $faker->boolean, 'ill' => $faker->numberBetween(0, 5), 'ill_comment' => $faker->sentence(), - 'mission' => function () { + 'mission_id' => function () { return factory(App\Mission::class)->create()->id; }, 'start' => $faker->dateTimeBetween('-180 days', '-90 days')->format('Y-m-d'), 'state' => 3, - 'user' => function () { + 'user_id' => function () { return factory(App\User::class)->create()->id; }, 'vacation' => $faker->numberBetween(0, 2), diff --git a/api/database/factories/UserFactory.php b/api/database/factories/UserFactory.php index 0335eda6..0ee78707 100755 --- a/api/database/factories/UserFactory.php +++ b/api/database/factories/UserFactory.php @@ -34,9 +34,11 @@ 'driving_licence_b' => $faker->boolean, 'driving_licence_be' => $faker->boolean, 'chainsaw_workshop' => $faker->boolean, - 'regional_center' => 2, + 'regional_center_id' => function () { + return factory(\App\RegionalCenter::class)->create()->id; + }, 'remember_token' => str_random(10), - 'role' => 2, + 'role_id' => 2, 'work_experience' => $faker->sentence(), 'zdp' => $faker->randomNumber(6), 'zip' => $faker->numberBetween(1000, 9999), @@ -45,14 +47,14 @@ $factory->defineAs(App\User::class, 'user_with_admin', function () use ($factory) { $user = $factory->raw(App\User::class); - $user['role'] = 1; + $user['role_id'] = 1; return $user; }); $factory->defineAs(App\User::class, 'admin', function () use ($factory) { $user = $factory->raw(App\User::class); - $user['email'] ='office@stiftungswo.ch'; + $user['email'] = 'office@stiftungswo.ch'; $user['password'] = app('hash')->make('GutesPasswort'); - $user['role'] = 1; + $user['role_id'] = 1; return $user; }); diff --git a/api/database/factories/UserFeedbackFactory.php b/api/database/factories/UserFeedbackFactory.php index cdee1c95..028c9992 100644 --- a/api/database/factories/UserFeedbackFactory.php +++ b/api/database/factories/UserFeedbackFactory.php @@ -9,7 +9,9 @@ 'answer' => $faker->sentence, 'feedbackId' => \Faker\Provider\Uuid::uuid(), 'questionId' => factory(\App\UserFeedbackQuestion::class, 'text_question_type')->create()->id, - 'user' => factory(\App\User::class)->create()->id, + 'user_id' => function () { + return factory(\App\User::class)->create()->id; + }, 'year' => $faker->dateTimeBetween('-365 days', '-90 days') ]; }); diff --git a/api/database/migrations/2019_01_08_154346_rename_foreign_keys.php b/api/database/migrations/2019_01_08_154346_rename_foreign_keys.php new file mode 100644 index 00000000..e11588ee --- /dev/null +++ b/api/database/migrations/2019_01_08_154346_rename_foreign_keys.php @@ -0,0 +1,82 @@ +renameColumn('holiday_type', 'holiday_type_id'); + }); + + Schema::table('missions', function (Blueprint $table) { + $table->renameColumn('specification', 'specification_id'); + $table->renameColumn('user', 'user_id'); + }); + + Schema::table('payment_entries', function (Blueprint $table) { + $table->renameColumn('payment', 'payment_id'); + $table->renameColumn('report_sheet', 'report_sheet_id'); + $table->renameColumn('user', 'user_id'); + }); + + Schema::table('report_sheets', function (Blueprint $table) { + $table->renameColumn('mission', 'mission_id'); + $table->renameColumn('user', 'user_id'); + }); + + Schema::table('user_feedbacks', function (Blueprint $table) { + $table->renameColumn('user', 'user_id'); + }); + + Schema::table('users', function (Blueprint $table) { + $table->renameColumn('regional_center', 'regional_center_id'); + $table->renameColumn('role', 'role_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('holidays', function (Blueprint $table) { + $table->renameColumn('holiday_type_id', 'holiday_type'); + }); + + Schema::table('missions', function (Blueprint $table) { + $table->renameColumn('specification_id', 'specification'); + $table->renameColumn('user_id', 'user'); + }); + + Schema::table('payment_entries', function (Blueprint $table) { + $table->renameColumn('payment_id', 'payment'); + $table->renameColumn('report_sheet_id', 'report_sheet'); + $table->renameColumn('user_id', 'user'); + }); + + Schema::table('report_sheets', function (Blueprint $table) { + $table->renameColumn('mission_id', 'mission'); + $table->renameColumn('user_id', 'user'); + }); + + Schema::table('user_feedbacks', function (Blueprint $table) { + $table->renameColumn('user_id', 'user'); + }); + + Schema::table('users', function (Blueprint $table) { + $table->renameColumn('regional_center_id', 'regional_center'); + $table->renameColumn('role_id', 'role'); + }); + } +} diff --git a/api/tests/TestCase.php b/api/tests/TestCase.php index dde675f2..b0f112f1 100755 --- a/api/tests/TestCase.php +++ b/api/tests/TestCase.php @@ -30,7 +30,7 @@ public function createApplication() public function asAdmin() { $user = factory(User::class)->create([ - 'role' => 1 + 'role_id' => 1 ]); return $this->actingAs($user); } @@ -43,7 +43,7 @@ public function asUser($user = null) { if (!$user) { $user = factory(User::class)->create([ - 'role' => 2 + 'role_id' => 2 ]); } return $this->actingAs($user); diff --git a/api/tests/Unit/HolidayTest.php b/api/tests/Unit/HolidayTest.php new file mode 100644 index 00000000..b47223f0 --- /dev/null +++ b/api/tests/Unit/HolidayTest.php @@ -0,0 +1,17 @@ +make(); + $holiday = factory(Holiday::class)->make(); + $holiday->holiday_type()->associate($holidayType); + $this->assertEquals($holidayType, $holiday->holiday_type); + } +} diff --git a/api/tests/integrations/AuthControllerTest.php b/api/tests/integrations/AuthControllerTest.php index f0754124..4f889835 100755 --- a/api/tests/integrations/AuthControllerTest.php +++ b/api/tests/integrations/AuthControllerTest.php @@ -133,7 +133,7 @@ public function testAlternateTokenParameter() $payload = [ 'iss' => "izivi-api", // Issuer of the token 'sub' => $user->id, // Subject of the token - 'isAdmin' => $user->role == 1, + 'isAdmin' => $user->isAdmin(), 'iat' => time(), // Time when JWT was issued. 'exp' => time() + 60*60*24, // Expiration time, ]; diff --git a/api/tests/integrations/MissionControllerTest.php b/api/tests/integrations/MissionControllerTest.php index c6555def..f26df10e 100644 --- a/api/tests/integrations/MissionControllerTest.php +++ b/api/tests/integrations/MissionControllerTest.php @@ -22,7 +22,7 @@ public function testValidUserPost() { $template = $this->missionTemplate(); $user = factory(User::class)->create(); - $template['user'] = $user->id; + $template['user_id'] = $user->id; $this->asUser($user)->json('POST', '/api/missions', $template)->assertResponseOk(); $this->assertResponseMatchesTemplate($template); @@ -41,12 +41,12 @@ public function testShouldUpdateReportSheets() $mission = Mission::latest()->first(); $mission->update(['draft' => '2018-11-05']); - $countOfMissions = ReportSheet::where('mission', "=", $mission->id)->count(); + $countOfMissions = ReportSheet::where('mission_id', "=", $mission->id)->count(); $template['end'] = '2021-12-31'; $this->asAdmin()->json('PUT', '/api/missions/' . $mission->id, $template); $this->assertResponseMatchesTemplate($template); - $this->assertEquals($countOfMissions + 12, ReportSheet::where('mission', "=", $mission->id)->count()); + $this->assertEquals($countOfMissions + 12, ReportSheet::where('mission_id', "=", $mission->id)->count()); } public function testShouldMarkMissionDraftAsReceived() @@ -95,12 +95,33 @@ public function testDeleteAsAdminWithValidId() $countBeforeMissionDeletion = count(Mission::all()); $countBeforeReportSheetDeletion = count(ReportSheet::all()); - $this->asAdmin()->json('DELETE', 'api/missions/' . $reportSheet->mission)->assertResponseOk(); + $this->asAdmin()->json('DELETE', 'api/missions/' . $reportSheet->mission_id)->assertResponseOk(); $this->assertCount($countBeforeMissionDeletion - 1, Mission::all()); $this->assertCount($countBeforeReportSheetDeletion - 1, ReportSheet::all()); } + public function testIndexByYearAsUser() + { + $this->asUser()->json('GET', 'api/missions/2020')->assertResponseStatus(401); + } + + public function testIndexByYearAsAdmin() + { + // should return all missions + factory(Mission::class, 10)->create([ + 'end' => function () { + return Carbon::parse('2020-01-01')->addWeeks(rand(26, 51)); + }, + 'start' => function () { + return Carbon::parse('2020-01-01')->addWeeks(rand(1, 25)); + } + ]); + $this->asAdmin()->json('GET', 'api/missions/2020')->assertResponseOk(); + $this->assertCount(Mission::whereDate('end', '>=', '2020-01-01') + ->whereDate('start', '<=', '2020-12-31')->count(), $this->responseToArray()); + } + private function missionTemplate() { return [ @@ -110,9 +131,9 @@ private function missionTemplate() 'long_mission' => false, 'mission_type' => rand(0, 2), 'probation_period' => false, - 'specification' => factory(Specification::class)->create()->id, + 'specification_id' => factory(Specification::class)->create()->id, 'start' => '2020-01-01', - 'user' => factory(User::class)->create()->id + 'user_id' => factory(User::class)->create()->id ]; } } diff --git a/api/tests/integrations/PDFControllerTest.php b/api/tests/integrations/PDFControllerTest.php index 3cd3bd76..7d34f67c 100644 --- a/api/tests/integrations/PDFControllerTest.php +++ b/api/tests/integrations/PDFControllerTest.php @@ -33,13 +33,13 @@ public function testGetPhoneList() ])->id; factory(\App\Mission::class, 2)->create([ - 'user' => $userWithoutMobileButPrivateId, + 'user_id' => $userWithoutMobileButPrivateId, 'start' => '2019-01-01', 'end' => '2019-04-01' ]); factory(\App\Mission::class, 2)->create([ - 'user' => $userWithoutMobileButBusinessId, + 'user_id' => $userWithoutMobileButBusinessId, 'start' => '2019-01-01', 'end' => '2019-04-01' ]); @@ -55,7 +55,7 @@ public function testGetZiviReportSheet() 'driving_charges_comment' => 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut', 'ill_comment' => 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut'// to test comment wrapping ]); - $zivi = \App\User::find($reportSheet->user); + $zivi = $reportSheet->user; $this->asUser($zivi)->json('GET', '/api/report_sheets/' . $reportSheet->id . '/download')->assertResponseOk(); $this->assertTrue($this->response->headers->get('content-type') == 'application/pdf'); @@ -121,15 +121,16 @@ public function testGetAufgebot() factory(Holiday::class)->create([ 'date_from' => '2020-05-06', 'date_to' => '2020-06-06', - 'holiday_type' => 1 + 'holiday_type_id' => 1 ]); + $mission = factory(\App\Mission::class)->create([ 'end' => '2020-12-31', 'start' => '2020-01-01' ]); // report sheet of a zivi should be available for himself - $zivi = \App\User::find($mission->user); + $zivi = $mission->user; $this->asUser($zivi)->json('GET', '/api/missions/' . $mission->id . '/draft')->assertResponseOk(); $this->assertTrue($this->response->headers->get('content-type') == 'application/pdf'); diff --git a/api/tests/integrations/PaymentControllerTest.php b/api/tests/integrations/PaymentControllerTest.php index ef06a234..0e724c25 100644 --- a/api/tests/integrations/PaymentControllerTest.php +++ b/api/tests/integrations/PaymentControllerTest.php @@ -3,12 +3,92 @@ namespace Tests\Integrations; use App\Payment; +use App\PaymentEntry; +use App\ReportSheet; +use App\User; use Laravel\Lumen\Testing\DatabaseTransactions; class PaymentControllerTest extends \TestCase { use DatabaseTransactions; + public function testGetAsUser() + { + $paymentId = factory(Payment::class)->create()->id; + $this->asUser()->json('GET', 'api/payments/' . $paymentId)->assertResponseStatus(401); + } + + public function testGetAsAdmin() + { + $payment = factory(Payment::class)->create(); + factory(PaymentEntry::class, 10)->create([ + 'payment_id' => $payment->id + ]); + + $this->asAdmin()->json('GET', 'api/payments/' . $payment->id)->assertResponseOk(); + $response = $this->responseToArray(); + + $this->assertCount(10, $response['sheets']); + } + + public function testGetXmlAsUser() + { + $paymentId = factory(Payment::class)->create()->id; + $this->asUser()->json('GET', 'api/payments/' . $paymentId . '/xml')->assertResponseStatus(401); + } + + public function testGetXmlAsAdmin() + { + $payment = factory(Payment::class)->create(); + + $this->asAdmin()->json('GET', 'api/payments/' . $payment->id . '/xml')->assertResponseOk(); + $this->assertTrue($this->response->headers->get('content-type') == 'application/xml'); + } + + public function testGetIsoPaymentXmlAsUser() + { + $this->asUser()->json('POST', 'api/payments/execute', [])->assertResponseStatus(401); + } + + public function testGetIsoPaymentXmlAsAdmin() + { + $reportSheet1 = factory(ReportSheet::class)->create(); + $user1 = $reportSheet1->user; + + $reportSheet2 = factory(ReportSheet::class)->create(); + $user2 = $reportSheet1->user; + + $this->asAdmin()->json('POST', 'api/payments/execute', [ + 'data' => [ + [ + 'address' => $user1->address, + 'amount' => 1000, + 'bic' => $user1->bank_bic, + 'city' => $user1->city, + 'first_name' => $user1->first_name, + 'iban' => $user1->bank_iban, + 'last_name' => $user1->last_name, + 'sheet_id' => $reportSheet1->id, + 'userid' => $user1->id, + 'zdp' => $user1->zdp, + 'zip' => $user1->zip + ], [ + 'address' => $user2->address, + 'amount' => 0, + 'bic' => $user2->bank_bic, + 'city' => $user2->city, + 'first_name' => $user2->first_name, + 'iban' => $user2->bank_iban, + 'last_name' => $user2->last_name, + 'sheet_id' => $reportSheet2->id, + 'userid' => $user2->id, + 'zdp' => $user2->zdp, + 'zip' => $user2->zip + ] + ] + ])->assertResponseOk(); + } + public function testIndexAsUser() { factory(Payment::class, 10)->create(); @@ -17,7 +97,11 @@ public function testIndexAsUser() public function testIndexAsAdmin() { - factory(Payment::class, 10)->create(); + factory(Payment::class, 10); + factory(ReportSheet::class, 10)->create([ + 'state' => 1 + ]); + $this->asAdmin()->json('GET', 'api/payments')->assertResponseOk(); } } diff --git a/api/tests/integrations/ReportSheetControllerTest.php b/api/tests/integrations/ReportSheetControllerTest.php index 9897d071..5e7ae1fa 100644 --- a/api/tests/integrations/ReportSheetControllerTest.php +++ b/api/tests/integrations/ReportSheetControllerTest.php @@ -51,11 +51,11 @@ public function testGetIndexAsUser() factory(ReportSheet::class)->create(); factory(ReportSheet::class)->create([ 'state' => 0, - 'user' => $user->id + 'user_id' => $user->id ]); $validReportSheet = factory(ReportSheet::class)->create([ 'state' => 3, - 'user' => $user->id + 'user_id' => $user->id ]); $this->asUser($user)->json('GET', 'api/report_sheets')->assertResponseOk(); @@ -67,6 +67,14 @@ public function testGetIndexAsUser() $this->assertArrayNotHasKey('work_comment', $response[0]); } + public function testIndexAsAdmin() + { + // should return all report sheets + factory(ReportSheet::class, 10)->create(); + $this->asAdmin()->json('GET', 'api/report_sheets')->assertResponseOk(); + $this->assertCount(ReportSheet::all()->count(), $this->responseToArray()); + } + public function testPutAsUser() { $reportSheet = factory(ReportSheet::class)->create()->toArray(); diff --git a/api/tests/integrations/UserControllerTest.php b/api/tests/integrations/UserControllerTest.php index f679fc8d..e0834927 100644 --- a/api/tests/integrations/UserControllerTest.php +++ b/api/tests/integrations/UserControllerTest.php @@ -19,7 +19,7 @@ public function testGetIndex() $response = $this->responseToArray(); $this->assertCount(count(User::all()), $response); $this->assertArrayHasKey('missions', $response[0]); - $this->assertArrayHasKey('user_role', $response[0]); + $this->assertArrayHasKey('role', $response[0]); } public function testGetAsUser()