Commit 20470ff6937135b997ac71816aa4a09d48af5d48

Authored by Etienne Pallier
2 parents 4a0780a9 92712462
Exists in master and in 1 other branch dev

Merge dev branch into master branch (vv3.7.9.66)

CHANGES.txt
... ... @@ -70,8 +70,9 @@ Outre ces changements, voici d'autres changements importants :
70 70 ======= CHANGES =======
71 71  
72 72 -------
73   -20/07/2020 v3.7.9.65 (EP)
74   - - (i) Ajouts important de TESTS sur les dates d'un matériel (achat, livraison, fin garantie)
  73 +21/07/2020 v3.7.9.65-66 (EP)
  74 + - (i) Ajouts important de TESTS sur les dates d'un matériel (achat, livraison, fin garantie)
  75 + - (i) Amélioration et refactorisation tests sur les dates
75 76  
76 77 -------
77 78 17/07/2020 v3.7.9.63-64 (EP)
... ...
README.md
... ... @@ -42,8 +42,8 @@ Logiciel testé et validé sur les configurations suivantes :
42 42  
43 43 --------------------------------------------------------------------------------------------
44 44  
45   -Date: 20/07/2020
46   -Version: 3.7.9.65
  45 +Date: 21/07/2020
  46 +Version: 3.7.9.66
47 47  
48 48  
49 49 HISTORIQUE DES CHANGEMENTS DE VERSION : voir le fichier CHANGES.txt (ou la page web /pages/changes)
... ...
src/Model/Entity/Materiel.php
... ... @@ -3,6 +3,9 @@ namespace App\Model\Entity;
3 3  
4 4 use Cake\ORM\Entity;
5 5  
  6 +// Max 10 ans entre 2 dates
  7 +const MAX_DIFF_YEARS = 10;
  8 +
6 9 /**
7 10 * Materiel Entity.
8 11 *
... ... @@ -85,6 +88,29 @@ class Materiel extends Entity {
85 88 // Ce qui s'affiche quand on fait echo $entity
86 89 public function __toString() { return $this->designation; }
87 90  
  91 + // date_reception >= date_acquisition et aussi date_fin_garantie >= date_reception
  92 + public function check_date_d2_gt_d1_but_not_too_much($d2_name,$d1_name) {
  93 + // Si une des 2 dates est nulle => return true
  94 + $d2 = $this->$d2_name;
  95 + $d1 = $this->$d1_name;
  96 + if (!$d2 || !$d1) return true;
  97 + $tz = new \DateTimeZone('Europe/Paris');
  98 + //date_default_timezone_set('Europe/Paris');
  99 + // DateTime lit les dates au format JJ-MM-YYYY (et non pas JJ/MM/YYYY)
  100 + //$d1 = ( new \DateTime(strtr($entity->date_acquisition,'/','-'),$tz) )->format('Ymd');
  101 + $d1 = new \DateTime(strtr($d1,'/','-'),$tz);
  102 + //$d1_text = $d1->format('Ymd');
  103 + $d2 = new \DateTime(strtr($d2,'/','-'),$tz);
  104 + //$d2_text = $d2->format('Ymd');
  105 + //$today = (new \DateTime('now',$tz))->format('Ymd');
  106 + //$ok = ($d2_text >= $d1_text);
  107 + if ($d2 < $d1) return false;
  108 + // $d2 > $d1 oui mais pas trop...
  109 + $diff = $d2->diff($d1);
  110 + //debug($diff->y);
  111 + return $diff->y < MAX_DIFF_YEARS;
  112 + }
  113 +
88 114 protected function hasStatus($status) { return $this->status == $status; }
89 115  
90 116 // (EP 20200504)
... ...
src/Model/Table/MaterielsTable.php
... ... @@ -9,6 +9,7 @@ use Cake\ORM\TableRegistry;
9 9 use Cake\I18n\Time;
10 10 use Cake\I18n\Date;
11 11 use Cake\ORM\Association\BelongsTo;
  12 +use Cake\ORM\Entity;
12 13  
13 14 /**
14 15 * Materiels Model
... ... @@ -40,7 +41,7 @@ use Cake\ORM\Association\BelongsTo;
40 41  
41 42  
42 43 // Max 10 ans entre 2 dates
43   -const MAX_DIFF_YEARS = 10;
  44 +//const MAX_DIFF_YEARS = 10;
44 45  
45 46 class MaterielsTable extends AppTable
46 47 {
... ... @@ -429,6 +430,29 @@ class MaterielsTable extends AppTable
429 430  
430 431 public function checkStatus($check) { return ($check !== null && in_array($check, $this->ALL_STATUS)); }
431 432  
  433 + /*
  434 + public function check_date_d2_gt_d1_but_not_too_much($d2_str,$d1_str) {
  435 + // Si une des 2 dates est nulle => return true
  436 + if (!$d2_str || !$d1_str) return true;
  437 + $tz = new \DateTimeZone('Europe/Paris');
  438 + // DateTime lit les dates au format JJ-MM-YYYY (et non pas JJ/MM/YYYY)
  439 + //$d1 = ( new \DateTime(strtr($entity->date_acquisition,'/','-'),$tz) )->format('Ymd');
  440 + $d1 = new \DateTime(strtr($d1_str,'/','-'),$tz);
  441 + //$d1_text = $d1->format('Ymd');
  442 + $d2 = new \DateTime(strtr($d2_str,'/','-'),$tz);
  443 + //$d2_text = $d2->format('Ymd');
  444 + //date_default_timezone_set('Europe/Paris');
  445 + //$today = (new \DateTime('now',$tz))->format('Ymd');
  446 + //$ok = ($d2_text >= $d1_text);
  447 + if ($d2 < $d1) return false;
  448 + // $d2 > $d1 oui mais pas trop...
  449 + $diff = $d2->diff($d1);
  450 + //debug($diff->y);
  451 + return $diff->y < MAX_DIFF_YEARS;
  452 + }
  453 + */
  454 +
  455 +
432 456 /**
433 457 * Returns a rules checker object that will be used for validating
434 458 * application integrity.
... ... @@ -491,7 +515,10 @@ class MaterielsTable extends AppTable
491 515  
492 516  
493 517 // Check dates
494   - $dateIsAfterDateAchatAndNotTooFar = function ($entity) {
  518 + $dateIsAfterDateAchatAndNotTooFar = function (Entity $entity) {
  519 + return $entity->check_date_d2_gt_d1_but_not_too_much('date_reception','date_acquisition');
  520 + //return $this->check_date_d2_gt_d1_but_not_too_much($entity->date_reception,$entity->date_acquisition);
  521 + /*
495 522 // Si une des 2 dates est nulle => return true
496 523 if (!$entity->date_reception || !$entity->date_acquisition) return true;
497 524 $tz = new \DateTimeZone('Europe/Paris');
... ... @@ -508,9 +535,13 @@ class MaterielsTable extends AppTable
508 535 // $d2 > $d1 oui mais pas trop...
509 536 $diff = $d2->diff($d1);
510 537 //debug($diff->y);
511   - return $diff->y < MAX_DIFF_YEARS;
  538 + return $diff->y < MAX_DIFF_YEARS;
  539 + */
512 540 };
513   - $dateIsAfterDateReceptionAndNotTooFar = function ($entity) {
  541 + $dateIsAfterDateReceptionAndNotTooFar = function (Entity $entity) {
  542 + return $entity->check_date_d2_gt_d1_but_not_too_much('date_fin_garantie','date_reception');
  543 + //return $this->check_date_d2_gt_d1_but_not_too_much($entity->date_fin_garantie,$entity->date_reception);
  544 + /*
514 545 // Si une des 2 dates est nulle => return true
515 546 if (!$entity->date_reception || !$entity->date_fin_garantie) return true;
516 547 $tz = new \DateTimeZone('Europe/Paris');
... ... @@ -526,6 +557,7 @@ class MaterielsTable extends AppTable
526 557 $diff = $d2->diff($d1);
527 558 //debug($diff->y);
528 559 return $diff->y < MAX_DIFF_YEARS;
  560 + */
529 561 };
530 562  
531 563 /*
... ...
tests/TestCase/Controller/MaterielsControllerTest.php
... ... @@ -22,6 +22,8 @@ use const App\Test\Fixture\yyyy2mmdd2;
22 22 use const App\Test\Fixture\yyyy3mmdd2;
23 23 use const App\Test\Fixture\yyyy1_highest_num;
24 24 use const App\Test\Fixture\yyyy2_highest_num;
  25 +use const App\Model\Entity\MAX_DIFF_YEARS;
  26 +//use const App\Model\Table\MAX_DIFF_YEARS;
25 27 /* marche pas, why ???
26 28 use const App\Test\Fixture\yyyy0;
27 29 use const App\Test\Fixture\yyyy1;
... ... @@ -2563,6 +2565,7 @@ class MaterielsControllerTest extends General {
2563 2565 //$flash_message = isset($_SESSION['Flash']) ? $_SESSION['Flash']['flash'][0]['message']:[];
2564 2566 //debug($_SESSION['Flash']);
2565 2567 $flash_message = isset($_SESSION['Flash']['flash']) ? $_SESSION['Flash']['flash'][0]['message'] : [];
  2568 + //debug($flash_message);
2566 2569 if ($SUCCESS && $expected_flash_message) $this->assertTrue($flash_message != [], "Pas de message flash alors qu'il devrait y en avoir un !");
2567 2570 if ($flash_message) {
2568 2571 // Assert a flash message in the 'flash' key.
... ... @@ -2594,6 +2597,7 @@ class MaterielsControllerTest extends General {
2594 2597 $full_action = "/$controller/$action_link";
2595 2598 if ($id) $full_action .= "/$id";
2596 2599 //debug($full_action);
  2600 + //debug($data);
2597 2601 $this->post($full_action, $data);
2598 2602 //if ($action =='statusValidated') return;
2599 2603 $this->assertResponseSuccess();
... ... @@ -2656,6 +2660,7 @@ class MaterielsControllerTest extends General {
2656 2660 * Test des controles sur les dates
2657 2661 *
2658 2662 */
  2663 + //const MAX_DIFF_YEARS = 10; // maxi 10 ans entre 2 dates
2659 2664 public function testMaterielDatesValidation() {
2660 2665  
2661 2666 // On teste avec le profil SUPERADMIN car il a (presque) tous les droits
... ... @@ -2681,83 +2686,110 @@ class MaterielsControllerTest extends General {
2681 2686  
2682 2687 // 2) Test edit date acquisition wrong format => KO
2683 2688  
2684   - // - date (achat) mauvais format => KO
  2689 + // - date avec tirets => KO
2685 2690 $new_data = [
2686 2691 $f => '19-04-'.yyyy0,
2687 2692 //'date_reception' => '',
2688 2693 ];
2689   - //$action = 'edit';
2690 2694 $this->_doActionAndCheckResult($action, $id, FALSE, $new_data);
2691   -
  2695 + // - année sur 2 chiffres => KO
  2696 + $new_data = [ $f => '19/04/'.(yyyy0-2000) ];
  2697 + $this->_doActionAndCheckResult($action, $id, FALSE, $new_data);
  2698 + // - date format US AA/MM/DD => KO
  2699 + $new_data = [ $f => yyyy0.'/04/19' ];
  2700 + $this->_doActionAndCheckResult($action, $id, FALSE, $new_data);
2692 2701 // - date (achat) incorrecte => KO
2693 2702 $new_data = [ $f => '31/04/'.yyyy0 ];
2694 2703 $this->_doActionAndCheckResult($action, $id, FALSE, $new_data);
  2704 +
  2705 +
  2706 + // 3) Test edit date acquisition non autorisée => KO
2695 2707  
2696 2708 // - date (achat) today => OK...
2697 2709 $tz = new \DateTimeZone('Europe/Paris');
2698 2710 $date = $today = new \DateTime('now',$tz);
2699 2711 //debug($date);
2700   - $new_data = [ $f => $date->format('d/m/Y') ];
  2712 + $new_data = [ $f => $date->format($format) ];
2701 2713 $this->_doActionAndCheckResult($action, $id, TRUE, $new_data);
2702   -
2703 2714 // - ... mais date future (demain) => KO
2704   - $tomorrow = new \DateTime('now',$tz);
2705   - $date = $tomorrow->add(new \DateInterval('P1D'));
  2715 + $date = $tomorrow = new \DateTime('+1 days',$tz);
  2716 + //$date = $tomorrow->add(new \DateInterval('P1D'));
2706 2717 //debug($date);
2707   - $new_data = [ $f => $date->format('d/m/Y') ];
  2718 + $new_data = [ $f => $date->format($format) ];
2708 2719 $this->_doActionAndCheckResult($action, $id, FALSE, $new_data);
2709   -
2710 2720 // - date (achat) loin dans le passé (-40 ans) => OK...
2711   - $old = new \DateTime('now',$tz);
2712   - $date = $old->sub(new \DateInterval('P40Y'));
2713   - $new_data = [ $f => $date->format('d/m/Y') ];
  2721 + $date = $old = new \DateTime('-40 years',$tz);
  2722 + //$old = new \DateTime('now',$tz);
  2723 + //$date = $old->sub(new \DateInterval('P40Y'));
  2724 + $new_data = [ $f => $date->format($format) ];
2714 2725 $this->_doActionAndCheckResult($action, $id, TRUE, $new_data);
2715 2726 // - mais pas trop loin dans le passé (-50 ans) => KO
2716 2727 $date = $old->sub(new \DateInterval('P10Y'));
2717 2728 //debug($date);
2718   - $new_data = [ $f => $date->format('d/m/Y') ];
  2729 + $new_data = [ $f => $date->format($format) ];
2719 2730 $this->_doActionAndCheckResult($action, $id, FALSE, $new_data);
2720 2731  
2721 2732  
2722   - // 3) Test Date reception >= date acq, mais pas trop loin
2723   - $f = 'date_reception';
2724   -
2725   - // - date (reception) < date acq => KO
2726   - $yesterday = new \DateTime('now',$tz);
2727   - $date = $yesterday->sub(new \DateInterval('P1D'));
2728   - $new_data = [
2729   - 'date_acquisition' => $today->format($format),
2730   - $f => $date->format($format)
2731   - ];
2732   - $this->_doActionAndCheckResult($action, $id, FALSE, $new_data);
2733   -
2734   - // - date (reception) = date acq => OK
2735   - $date = $today;
2736   - $new_data = [
2737   - 'date_acquisition' => $today->format($format),
2738   - $f => $date->format($format)
2739   - ];
2740   - $this->_doActionAndCheckResult($action, $id, TRUE, $new_data);
2741   -
2742   - // - date (reception) > date_acq (jqa MAX_DIFF - 1 jour) => OK...
2743   - $MAX_DIFF = 10;
2744   - $far = new \DateTime('now',$tz);
2745   - $far->add(new \DateInterval('P'.$MAX_DIFF.'Y'));
2746   - $date = $far->sub(new \DateInterval('P1D'));
2747   - debug($date);
2748   - $new_data = [
2749   - 'date_acquisition' => $today->format('d/m/Y'),
2750   - $f => $date->format('d/m/Y')
  2733 + // 4) Test écart entre 2 dates :
  2734 + // - a) Date reception >= date acq, mais pas trop loin
  2735 + // - b) Date fin garantie >= date reception, mais pas trop loin
  2736 + $dates = [
  2737 + 'rec-acq' => [
  2738 + 'date_acquisition',
  2739 + 'date_reception'
  2740 + ],
  2741 + 'gar-rec' => [
  2742 + 'date_reception',
  2743 + 'date_fin_garantie'
  2744 + ]
2751 2745 ];
2752   - $this->_doActionAndCheckResult($action, $id, TRUE, $new_data);
2753   - // - ...mais pas trop loin dans le futur (MAX_DIFF) => KO
2754   - $date = $far->add(new \DateInterval('P1D'));
2755   - debug($date);
2756   - $new_data = [ $f => $date->format('d/m/Y') ];
2757   - $this->_doActionAndCheckResult($action, $id, FALSE, $new_data);
2758   -
2759   -
2760   - // 4) Test Date fin garantie >= date reception, mais pas trop loin
  2746 + foreach ($dates as $d) {
  2747 + //$d1 = 'date_acquisition';
  2748 + //$d2 = 'date_reception';
  2749 + $d1 = $d[0];
  2750 + $d2 = $d[1];
  2751 + // - date (reception) < date acq => KO
  2752 + $date = $yesterday = new \DateTime('-1 days',$tz);
  2753 + /*
  2754 + $yesterday = new \DateTime('now',$tz);
  2755 + $date = $yesterday->sub(new \DateInterval('P1D'));
  2756 + */
  2757 + $new_data = [
  2758 + $d1 => $today->format($format),
  2759 + $d2 => $date->format($format)
  2760 + ];
  2761 + $this->_doActionAndCheckResult($action, $id, FALSE, $new_data);
  2762 + // - date (reception) = date acq => OK
  2763 + $date = $today;
  2764 + $new_data = [
  2765 + $d1 => $today->format($format),
  2766 + $d2 => $date->format($format)
  2767 + ];
  2768 + $this->_doActionAndCheckResult($action, $id, TRUE, $new_data);
  2769 + // - date (reception) > date_acq (jqa MAX_DIFF - 1 jour) => OK...
  2770 + /*
  2771 + $far = new \DateTime('now',$tz);
  2772 + $far->add(new \DateInterval('P'.MAX_DIFF_YEARS.'Y'));
  2773 + */
  2774 + $far = new \DateTime('+'.MAX_DIFF_YEARS.' years',$tz);
  2775 + $date = $far->sub(new \DateInterval('P1D'));
  2776 + //debug($date);
  2777 + $new_data = [
  2778 + $d1 => $today->format($format),
  2779 + $d2 => $date->format($format)
  2780 + ];
  2781 + $this->_doActionAndCheckResult($action, $id, TRUE, $new_data);
  2782 + // - ...mais pas trop loin dans le futur (MAX_DIFF) => KO
  2783 + $date = $far->add(new \DateInterval('P1D'));
  2784 + //debug($date);
  2785 + $new_data = [
  2786 + $d1 => $today->format($format),
  2787 + $d2 => $date->format($format)
  2788 + ];
  2789 + $this->_doActionAndCheckResult($action, $id, FALSE, $new_data);
  2790 + } // foreach $dates
  2791 +
  2792 + // 5) Test Date fin garantie >= date reception, mais pas trop loin
2761 2793 // C'est la meme chose que le point precedent (3), on suppose donc que c'est ok...
2762 2794 }
2763 2795  
... ...