UtilsTest.php 19 KB

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