UtilsTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. /** @noinspection PhpUnhandledExceptionInspection */
  3. namespace App\Tests\Unit\Service\Organization;
  4. use App\Entity\Organization\Organization;
  5. use App\Entity\Organization\Parameters;
  6. use App\Entity\Organization\Settings;
  7. use App\Entity\Organization\Subdomain;
  8. use App\Enum\Organization\OrganizationIdsEnum;
  9. use App\Enum\Organization\SettingsProductEnum;
  10. use App\Service\Organization\Utils as OrganizationUtils;
  11. use App\Service\Utils\DatesUtils;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use PHPUnit\Framework\TestCase;
  14. class TestableOrganizationUtils extends OrganizationUtils
  15. {
  16. public function isOrganizationIdEqualTo(Organization $organization, OrganizationIdsEnum $organizationIdsEnum): bool
  17. {
  18. return parent::isOrganizationIdEqualTo($organization, $organizationIdsEnum);
  19. }
  20. }
  21. class UtilsTest extends TestCase
  22. {
  23. /**
  24. * @see OrganizationUtils::isStructure()
  25. */
  26. public function testIsStructure(): void
  27. {
  28. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['isStructure'])->getMock();
  29. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
  30. $this->assertTrue($organizationUtils->isStructure($organization));
  31. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
  32. $this->assertTrue($organizationUtils->isStructure($organization));
  33. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
  34. $this->assertTrue($organizationUtils->isStructure($organization));
  35. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
  36. $this->assertTrue($organizationUtils->isStructure($organization));
  37. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
  38. $this->assertFalse($organizationUtils->isStructure($organization));
  39. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
  40. $this->assertFalse($organizationUtils->isStructure($organization));
  41. }
  42. /**
  43. * @see OrganizationUtils::isManager()
  44. */
  45. public function testIsManager(): void
  46. {
  47. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['isManager'])->getMock();
  48. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
  49. $this->assertFalse($organizationUtils->isManager($organization));
  50. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
  51. $this->assertFalse($organizationUtils->isManager($organization));
  52. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
  53. $this->assertFalse($organizationUtils->isManager($organization));
  54. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
  55. $this->assertFalse($organizationUtils->isManager($organization));
  56. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
  57. $this->assertTrue($organizationUtils->isManager($organization));
  58. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
  59. $this->assertFalse($organizationUtils->isManager($organization));
  60. }
  61. /**
  62. * @see OrganizationUtils::is2iosOrganization()
  63. */
  64. public function testIsOrganizationIs2ios(): void
  65. {
  66. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['is2iosOrganization'])->getMock();
  67. $organization = $this->getMockBuilder(Organization::class)->getMock();
  68. $organizationUtils->expects(self::once())->method('isOrganizationIdEqualTo')->with($organization, OrganizationIdsEnum::_2IOS);
  69. $organizationUtils->is2iosOrganization($organization);
  70. }
  71. /**
  72. * @see OrganizationUtils::isOrganizationCMF()
  73. */
  74. public function testIsOrganizationIsCMF(): void
  75. {
  76. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['isOrganizationCMF'])->getMock();
  77. $organization = $this->getMockBuilder(Organization::class)->getMock();
  78. $organizationUtils->expects(self::once())->method('isOrganizationIdEqualTo')->with($organization, OrganizationIdsEnum::CMF);
  79. $organizationUtils->isOrganizationCMF($organization);
  80. }
  81. /**
  82. * @see OrganizationUtils::is2iosOrganization()
  83. */
  84. public function testIsOrganizationIdEqualTo(): void
  85. {
  86. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['isOrganizationIdEqualTo'])->getMock();
  87. $organization = $this->getMockBuilder(Organization::class)->getMock();
  88. $organization->method('getId')->willReturnOnConsecutiveCalls(123, OrganizationIdsEnum::_2IOS->value);
  89. $this->assertFalse($organizationUtils->isOrganizationIdEqualTo($organization, OrganizationIdsEnum::_2IOS));
  90. $this->assertTrue($organizationUtils->isOrganizationIdEqualTo($organization, OrganizationIdsEnum::_2IOS));
  91. }
  92. /**
  93. * @see OrganizationUtils::getOrganizationCurrentActivityYear()
  94. */
  95. public function testGetOrganizationCurrentActivityYear(): void
  96. {
  97. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getOrganizationCurrentActivityYear'])->getMock();
  98. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  99. $parameters->method('getMusicalDate')->willReturnOnConsecutiveCalls(
  100. null, // default should be '2020-08-31'
  101. new \DateTime('2020-10-01')
  102. );
  103. $organization = $this->getMockBuilder(Organization::class)->getMock();
  104. $organization->method('getParameters')->willReturn($parameters);
  105. DatesUtils::setFakeDatetime('2020-03-01');
  106. $this->assertEquals(
  107. 2019,
  108. $organizationUtils->getOrganizationCurrentActivityYear($organization)
  109. );
  110. DatesUtils::setFakeDatetime('2020-11-01');
  111. $this->assertEquals(
  112. 2020,
  113. $organizationUtils->getOrganizationCurrentActivityYear($organization)
  114. );
  115. }
  116. /**
  117. * @see OrganizationUtils::getActivityPeriodsSwitchYear()
  118. */
  119. public function testGetActivityPeriodsSwitchYear(): void
  120. {
  121. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getActivityPeriodsSwitchYear'])->getMock();
  122. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  123. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2020-09-05'));
  124. $organization = $this->getMockBuilder(Organization::class)->getMock();
  125. $organization->method('getParameters')->willReturn($parameters);
  126. $periods = $organizationUtils->getActivityPeriodsSwitchYear($organization, 2022);
  127. $this->assertEquals('2022-09-05', $periods['dateStart']);
  128. $this->assertEquals('2023-09-04', $periods['dateEnd']);
  129. }
  130. /**
  131. * @see OrganizationUtils::getActivityPeriodsSwitchYear()
  132. */
  133. public function testGetActivityPeriodsSwitchYearNoMusicalDate(): void
  134. {
  135. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getActivityPeriodsSwitchYear'])->getMock();
  136. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  137. $parameters->method('getMusicalDate')->willReturn(null);
  138. $organization = $this->getMockBuilder(Organization::class)->getMock();
  139. $organization->method('getParameters')->willReturn($parameters);
  140. $periods = $organizationUtils->getActivityPeriodsSwitchYear($organization, 2022);
  141. $this->assertEquals('2022-09-01', $periods['dateStart']);
  142. $this->assertEquals('2023-08-31', $periods['dateEnd']);
  143. }
  144. /**
  145. * @see OrganizationUtils::getActivityYearSwitchDate()
  146. */
  147. public function testGetActivityYearSwitchDate(): void
  148. {
  149. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getActivityYearSwitchDate'])->getMock();
  150. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  151. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2020-09-05'));
  152. $organization = $this->getMockBuilder(Organization::class)->getMock();
  153. $organization->method('getParameters')->willReturn($parameters);
  154. $this->assertEquals(2022, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-09-10')));
  155. $this->assertEquals(2021, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-09-02')));
  156. }
  157. /**
  158. * @see OrganizationUtils::getActivityYearSwitchDate()
  159. */
  160. public function testGetActivityYearSwitchDateNoMusicalDate(): void
  161. {
  162. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getActivityYearSwitchDate'])->getMock();
  163. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  164. $parameters->method('getMusicalDate')->willReturn(null);
  165. $organization = $this->getMockBuilder(Organization::class)->getMock();
  166. $organization->method('getParameters')->willReturn($parameters);
  167. $this->assertEquals(2021, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-08-01')));
  168. $this->assertEquals(2022, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-09-02')));
  169. }
  170. /**
  171. * @see OrganizationUtils::getOrganizationActiveSubdomain()
  172. */
  173. public function testGetOrganizationActiveSubdomain(): void
  174. {
  175. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getOrganizationActiveSubdomain'])->getMock();
  176. $subdomain1 = $this->getMockBuilder(Subdomain::class)->getMock();
  177. $subdomain1->method('isActive')->willReturn(false);
  178. $subdomain2 = $this->getMockBuilder(Subdomain::class)->getMock();
  179. $subdomain2->method('isActive')->willReturn(false);
  180. $subdomain3 = $this->getMockBuilder(Subdomain::class)->getMock();
  181. $subdomain3->method('isActive')->willReturn(true);
  182. $subdomain3->method('getSubdomain')->willReturn('foo');
  183. $organization = $this->getMockBuilder(Organization::class)->getMock();
  184. $organization->method('getSubdomains')->willReturn(new ArrayCollection([$subdomain1, $subdomain2, $subdomain3]));
  185. $this->assertEquals('foo', $organizationUtils->getOrganizationActiveSubdomain($organization));
  186. }
  187. /**
  188. * @see OrganizationUtils::getOrganizationActiveSubdomain()
  189. */
  190. public function testGetOrganizationActiveSubdomainNone(): void
  191. {
  192. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getOrganizationActiveSubdomain'])->getMock();
  193. $subdomain1 = $this->getMockBuilder(Subdomain::class)->getMock();
  194. $subdomain1->method('isActive')->willReturn(false);
  195. $organization = $this->getMockBuilder(Organization::class)->getMock();
  196. $organization->method('getSubdomains')->willReturn(new ArrayCollection([$subdomain1]));
  197. $this->assertEquals(null, $organizationUtils->getOrganizationActiveSubdomain($organization));
  198. }
  199. /**
  200. * @see OrganizationUtils::getOrganizationWebsite()
  201. */
  202. public function testOrganizationWebsite(): void
  203. {
  204. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getOrganizationWebsite'])->getMock();
  205. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  206. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  207. $parameters->method('getCustomDomain')->willReturn(null);
  208. $organization = $this->getMockBuilder(Organization::class)->getMock();
  209. $organization->method('getParameters')->willReturn($parameters);
  210. $organizationUtils->method('getOrganizationActiveSubdomain')->with($organization)->willReturn('foo');
  211. $this->assertEquals('https://foo.opentalent.fr', $organizationUtils->getOrganizationWebsite($organization));
  212. }
  213. /**
  214. * @see OrganizationUtils::getOrganizationWebsite()
  215. */
  216. public function testOrganizationWebsiteDeactivatedWithOther(): void
  217. {
  218. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getOrganizationWebsite'])->getMock();
  219. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  220. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  221. $parameters->method('getWebsite')->willReturn('foo.net');
  222. $organization = $this->getMockBuilder(Organization::class)->getMock();
  223. $organization->method('getParameters')->willReturn($parameters);
  224. $this->assertEquals('https://foo.net', $organizationUtils->getOrganizationWebsite($organization));
  225. }
  226. /**
  227. * @see OrganizationUtils::getOrganizationWebsite()
  228. */
  229. public function testOrganizationWebsiteDeactivatedNoOther(): void
  230. {
  231. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getOrganizationWebsite'])->getMock();
  232. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  233. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  234. $parameters->method('getWebsite')->willReturn(null);
  235. $organization = $this->getMockBuilder(Organization::class)->getMock();
  236. $organization->method('getParameters')->willReturn($parameters);
  237. $this->assertEquals(null, $organizationUtils->getOrganizationWebsite($organization));
  238. }
  239. /**
  240. * @see OrganizationUtils::getOrganizationWebsite()
  241. */
  242. public function testOrganizationWebsiteWithCustom(): void
  243. {
  244. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getOrganizationWebsite'])->getMock();
  245. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  246. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  247. $parameters->method('getCustomDomain')->willReturn('bar.net');
  248. $organization = $this->getMockBuilder(Organization::class)->getMock();
  249. $organization->method('getParameters')->willReturn($parameters);
  250. $this->assertEquals('https://bar.net', $organizationUtils->getOrganizationWebsite($organization));
  251. }
  252. /**
  253. * @see OrganizationUtils::getOrganizationWebsite()
  254. */
  255. public function testOrganizationWebsiteNoSubdomains(): void
  256. {
  257. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['getOrganizationWebsite'])->getMock();
  258. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  259. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  260. $parameters->method('getCustomDomain')->willReturn(null);
  261. $organization = $this->getMockBuilder(Organization::class)->getMock();
  262. $organization->method('getParameters')->willReturn($parameters);
  263. $organizationUtils->method('getOrganizationActiveSubdomain')->with($organization)->willReturn(null);
  264. $this->assertEquals(null, $organizationUtils->getOrganizationWebsite($organization));
  265. }
  266. /**
  267. * @see OrganizationUtils::isSchool()
  268. */
  269. public function testIsSchool(): void
  270. {
  271. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['isSchool'])->getMock();
  272. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
  273. $this->assertFalse($organizationUtils->isSchool($organization));
  274. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
  275. $this->assertFalse($organizationUtils->isSchool($organization));
  276. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
  277. $this->assertTrue($organizationUtils->isSchool($organization));
  278. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
  279. $this->assertTrue($organizationUtils->isSchool($organization));
  280. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
  281. $this->assertFalse($organizationUtils->isSchool($organization));
  282. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
  283. $this->assertFalse($organizationUtils->isSchool($organization));
  284. }
  285. /**
  286. * @see OrganizationUtils::isArtist()
  287. */
  288. public function testIsArtist(): void
  289. {
  290. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['isArtist'])->getMock();
  291. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
  292. $this->assertTrue($organizationUtils->isArtist($organization));
  293. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
  294. $this->assertTrue($organizationUtils->isArtist($organization));
  295. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
  296. $this->assertFalse($organizationUtils->isArtist($organization));
  297. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
  298. $this->assertFalse($organizationUtils->isArtist($organization));
  299. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
  300. $this->assertFalse($organizationUtils->isArtist($organization));
  301. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
  302. $this->assertFalse($organizationUtils->isArtist($organization));
  303. }
  304. /**
  305. * @see OrganizationUtils::hasModule()
  306. */
  307. public function testHasModule(): void
  308. {
  309. $organizationUtils = $this->getMockBuilder(TestableOrganizationUtils::class)->setMethodsExcept(['hasModule'])->getMock();
  310. $settings = $this->getMockBuilder(Settings::class)->getMock();
  311. $settings->method('getModules')->willReturn(['foo', 'bar']);
  312. $organization = $this->getMockBuilder(Organization::class)->getMock();
  313. $organization->method('getSettings')->willReturn($settings);
  314. $this->assertTrue($organizationUtils->hasModule($organization, 'foo'));
  315. $this->assertFalse($organizationUtils->hasModule($organization, 'other'));
  316. }
  317. private function createOrganizationWithProductMock(SettingsProductEnum $product): Organization
  318. {
  319. $settings = $this->getMockBuilder(Settings::class)->getMock();
  320. $settings->method('getProduct')->willReturn($product);
  321. $organization = $this->getMockBuilder(Organization::class)->getMock();
  322. $organization->method('getSettings')->willReturn($settings);
  323. return $organization;
  324. }
  325. }