Prechádzať zdrojové kódy

Merge branch 'hotfix/V8-3226'

Olivier Massot 3 rokov pred
rodič
commit
07905df75e

+ 2 - 0
src/Enum/Network/NetworkEnum.php

@@ -7,6 +7,8 @@ use MyCLabs\Enum\Enum;
 
 /**
  * Liste des réseaux disponibles
+ * @method static CMF()
+ * @method static FFEC()
  */
 class NetworkEnum extends Enum
 {

+ 21 - 4
src/Service/Dolibarr/DolibarrSyncService.php

@@ -10,6 +10,7 @@ use App\Entity\Person\Person;
 use App\Enum\Access\FunctionEnum;
 use App\Enum\Access\RoleEnum;
 use App\Enum\Core\ContactPointTypeEnum;
+use App\Enum\Network\NetworkEnum;
 use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
 use App\Enum\Organization\OrganizationIdsEnum;
 use App\Enum\Organization\SettingsProductEnum;
@@ -129,11 +130,11 @@ class DolibarrSyncService
             $newSocietyData['phone'] = $this->getOrganizationPhone($organization);
 
             // Sync Network
-            $newSocietyData['parent'] = match (
-                $organization->getNetworkOrganizations()?->first()?->getNetwork()?->getId()
+            $newSocietyData['parent'] = '' . match (
+                $this->getOrganizationNetworkId($organization)
             ) {
-                OrganizationIdsEnum::CMF()->getValue() => $cmfDolibarrId,
-                OrganizationIdsEnum::FFEC()->getValue() => $ffecDolibarrId,
+                NetworkEnum::CMF()->getValue() => $cmfDolibarrId,
+                NetworkEnum::FFEC()->getValue() => $ffecDolibarrId,
                 default => null
             };
 
@@ -587,6 +588,22 @@ class DolibarrSyncService
         return null;
     }
 
+    /**
+     * Return the id of the first active network found for the given organization
+     *
+     * @param Organization $organization
+     * @return int|null
+     */
+    protected function getOrganizationNetworkId(Organization $organization): ?int {
+        foreach ($organization->getNetworkOrganizations() as $networkOrganization) {
+            if ($networkOrganization->getEndDate() !== null && $networkOrganization->getEndDate() < new \DateTime()) {
+                continue;
+            }
+            return $networkOrganization->getNetwork()?->getId();
+        }
+        return null;
+    }
+
     /**
      * Returns the number of accesses possessing at least one of the missions
      *

+ 63 - 2
tests/Service/Dolibarr/DolibarrSyncServiceTest.php

@@ -41,6 +41,7 @@ class TestableDolibarrSyncService extends DolibarrSyncService {
     public function getOrganizationPostalAddress(Organization $organization): ?AddressPostal { return parent::getOrganizationPostalAddress($organization); }
     public function getOrganizationPhone(Organization $organization): ?string { return parent::getOrganizationPhone($organization); }
     public function getOrganizationEmail(Organization $organization): ?string { return parent::getOrganizationEmail($organization); }
+    public function getOrganizationNetworkId(Organization $organization): ?int { return parent::getOrganizationNetworkId($organization); }
     public static function countWithMission(array $missions, array $members): int { return parent::countWithMission($missions, $members); }
     public function getPersonContact(Person $person): ?ContactPoint { return parent::getPersonContact($person); }
     public function formatContactPosition(array $missions, ?string $gender = 'X'): string { return parent::formatContactPosition($missions, $gender); }
@@ -176,7 +177,7 @@ class DolibarrSyncServiceTest extends TestCase
 
         // Network
         $network = $this->getMockBuilder(Network::class)->getMock();
-        $network->method('getId')->willReturn(91295);
+        $network->method('getId')->willReturn(3);
         $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
         $networkOrganization->method('getNetwork')->willReturn($network);
         $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
@@ -278,7 +279,7 @@ class DolibarrSyncServiceTest extends TestCase
                 'town : `CLUSES` => `Londres`',
                 'email : `` => `email@email.com`',
                 'phone : `+33678403010` => `+33102030405`',
-                'parent : `` => `5086`',
+                'parent : `` => `711`',
                 "array_options.options_2iopeninfoopentalent : `` => `Nombre d'élèves : 1\nNombre d'adhérents : 3\nNombre d'accès admin : 1`"
             ],
             $operations[0]->getChangeLog()
@@ -613,6 +614,66 @@ class DolibarrSyncServiceTest extends TestCase
         );
     }
 
+    public function testGetOrganizationNetworkId() {
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $network = $this->getMockBuilder(Network::class)->getMock();
+        $network->method('getId')->willReturn(3);
+        $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $networkOrganization->method('getNetwork')->willReturn($network);
+        $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
+
+        $syncService = $this->newDolibarrSyncService();
+
+        $this->assertEquals(
+            3,
+            $syncService->getOrganizationNetworkId($organization)
+        );
+    }
+
+    public function testGetOrganizationNetworkIdWithMultipleResult() {
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+
+        $network1 = $this->getMockBuilder(Network::class)->getMock();
+        $network1->method('getId')->willReturn(3);
+        $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $networkOrganization1->method('getNetwork')->willReturn($network1);
+        $networkOrganization1->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
+
+        $network2 = $this->getMockBuilder(Network::class)->getMock();
+        $network2->method('getId')->willReturn(4);
+        $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $networkOrganization2->method('getNetwork')->willReturn($network2);
+        $networkOrganization2->method('getEndDate')->willReturn(null);
+
+        $organization->method('getNetworkOrganizations')->willReturn(
+            new ArrayCollection([$networkOrganization1, $networkOrganization2])
+        );
+
+        $syncService = $this->newDolibarrSyncService();
+
+        $this->assertEquals(
+            4,
+            $syncService->getOrganizationNetworkId($organization)
+        );
+    }
+
+    public function testGetOrganizationNetworkIdWithNoResult() {
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $network = $this->getMockBuilder(Network::class)->getMock();
+        $network->method('getId')->willReturn(3);
+        $networkOrganization = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $networkOrganization->method('getNetwork')->willReturn($network);
+        $networkOrganization->method('getEndDate')->willReturn(new \DateTime('2000-01-01'));
+        $organization->method('getNetworkOrganizations')->willReturn(new ArrayCollection([$networkOrganization]));
+
+        $syncService = $this->newDolibarrSyncService();
+
+        $this->assertEquals(
+            null,
+            $syncService->getOrganizationNetworkId($organization)
+        );
+    }
+
     public function testCountWithMission() {
         $members = [
             123 => [FunctionEnum::PRESIDENT()->getValue(), FunctionEnum::TEACHER()->getValue()],