UtilsTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. <?php
  2. /** @noinspection PhpUnhandledExceptionInspection */
  3. namespace App\Tests\Unit\Service\Organization;
  4. use App\Entity\Network\NetworkOrganization;
  5. use App\Entity\Organization\Organization;
  6. use App\Entity\Organization\Parameters;
  7. use App\Entity\Organization\Settings;
  8. use App\Entity\Organization\Subdomain;
  9. use App\Enum\Organization\OrganizationIdsEnum;
  10. use App\Enum\Organization\SettingsProductEnum;
  11. use App\Repository\Network\NetworkOrganizationRepository;
  12. use App\Service\Network\Utils as NetworkUtils;
  13. use App\Service\Organization\Utils as OrganizationUtils;
  14. use App\Service\Utils\DatesUtils;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use PHPUnit\Framework\TestCase;
  18. class UtilsTest extends TestCase
  19. {
  20. private MockObject|NetworkOrganizationRepository $networkOrganizationRepository;
  21. private MockObject|NetworkUtils $networkUtils;
  22. public function setUp(): void
  23. {
  24. $this->networkOrganizationRepository = $this->getMockBuilder(NetworkOrganizationRepository::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $this->networkUtils = $this->getMockBuilder(NetworkUtils::class)->disableOriginalConstructor()->getMock();
  28. }
  29. public function getOrganizationUtilsMockFor(string $methodName): MockObject|OrganizationUtils
  30. {
  31. return $this->getMockBuilder(OrganizationUtils::class)
  32. ->setConstructorArgs([$this->networkOrganizationRepository, $this->networkUtils])
  33. ->setMethodsExcept([$methodName])
  34. ->getMock();
  35. }
  36. private function createOrganizationWithProductMock(SettingsProductEnum $product): Organization
  37. {
  38. $settings = $this->getMockBuilder(Settings::class)->getMock();
  39. $settings->method('getProduct')->willReturn($product);
  40. $organization = $this->getMockBuilder(Organization::class)->getMock();
  41. $organization->method('getSettings')->willReturn($settings);
  42. return $organization;
  43. }
  44. /**
  45. * @see OrganizationUtils::isStructure()
  46. */
  47. public function testIsStructure(): void
  48. {
  49. $organizationUtils = $this->getOrganizationUtilsMockFor('isStructure');
  50. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
  51. $this->assertTrue($organizationUtils->isStructure($organization));
  52. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
  53. $this->assertTrue($organizationUtils->isStructure($organization));
  54. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
  55. $this->assertTrue($organizationUtils->isStructure($organization));
  56. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
  57. $this->assertTrue($organizationUtils->isStructure($organization));
  58. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
  59. $this->assertFalse($organizationUtils->isStructure($organization));
  60. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
  61. $this->assertFalse($organizationUtils->isStructure($organization));
  62. }
  63. /**
  64. * @see OrganizationUtils::isManager()
  65. */
  66. public function testIsManager(): void
  67. {
  68. $organizationUtils = $this->getOrganizationUtilsMockFor('isManager');
  69. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
  70. $this->assertFalse($organizationUtils->isManager($organization));
  71. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
  72. $this->assertFalse($organizationUtils->isManager($organization));
  73. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
  74. $this->assertFalse($organizationUtils->isManager($organization));
  75. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
  76. $this->assertFalse($organizationUtils->isManager($organization));
  77. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
  78. $this->assertTrue($organizationUtils->isManager($organization));
  79. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
  80. $this->assertFalse($organizationUtils->isManager($organization));
  81. }
  82. /**
  83. * @see OrganizationUtils::is2iosOrganization()
  84. */
  85. public function testIsOrganizationIs2ios(): void
  86. {
  87. $organizationUtils = $this->getOrganizationUtilsMockFor('is2iosOrganization');
  88. $organization = $this->getMockBuilder(Organization::class)->getMock();
  89. $organizationUtils->expects(self::once())->method('isOrganizationIdEqualTo')->with($organization, OrganizationIdsEnum::_2IOS);
  90. $organizationUtils->is2iosOrganization($organization);
  91. }
  92. /**
  93. * @see OrganizationUtils::isCMF()
  94. */
  95. public function testIsCMF(): void
  96. {
  97. $organizationUtils = $this->getOrganizationUtilsMockFor('isCMF');
  98. $organization = $this->getMockBuilder(Organization::class)->getMock();
  99. $organizationUtils->expects(self::once())->method('isOrganizationIdEqualTo')->with($organization, OrganizationIdsEnum::CMF);
  100. $organizationUtils->isCMF($organization);
  101. }
  102. /**
  103. * @see OrganizationUtils::is2iosOrganization()
  104. */
  105. public function testIsOrganizationIdEqualTo(): void
  106. {
  107. $organizationUtils = $this->getOrganizationUtilsMockFor('isOrganizationIdEqualTo');
  108. $organization = $this->getMockBuilder(Organization::class)->getMock();
  109. $organization->method('getId')->willReturnOnConsecutiveCalls(123, OrganizationIdsEnum::_2IOS->value);
  110. $this->assertFalse($organizationUtils->isOrganizationIdEqualTo($organization, OrganizationIdsEnum::_2IOS));
  111. $this->assertTrue($organizationUtils->isOrganizationIdEqualTo($organization, OrganizationIdsEnum::_2IOS));
  112. }
  113. /**
  114. * @see OrganizationUtils::getOrganizationCurrentActivityYear()
  115. */
  116. public function testGetOrganizationCurrentActivityYear(): void
  117. {
  118. $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationCurrentActivityYear');
  119. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  120. $parameters->method('getMusicalDate')->willReturnOnConsecutiveCalls(
  121. null, // default should be '2020-08-31'
  122. new \DateTime('2020-10-01')
  123. );
  124. $organization = $this->getMockBuilder(Organization::class)->getMock();
  125. $organization->method('getParameters')->willReturn($parameters);
  126. DatesUtils::setFakeDatetime('2020-03-01');
  127. $this->assertEquals(
  128. 2019,
  129. $organizationUtils->getOrganizationCurrentActivityYear($organization)
  130. );
  131. DatesUtils::setFakeDatetime('2020-11-01');
  132. $this->assertEquals(
  133. 2020,
  134. $organizationUtils->getOrganizationCurrentActivityYear($organization)
  135. );
  136. }
  137. /**
  138. * @see OrganizationUtils::getActivityPeriodsSwitchYear()
  139. */
  140. public function testGetActivityPeriodsSwitchYear(): void
  141. {
  142. $organizationUtils = $this->getOrganizationUtilsMockFor('getActivityPeriodsSwitchYear');
  143. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  144. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2020-09-05'));
  145. $organization = $this->getMockBuilder(Organization::class)->getMock();
  146. $organization->method('getParameters')->willReturn($parameters);
  147. $periods = $organizationUtils->getActivityPeriodsSwitchYear($organization, 2022);
  148. $this->assertEquals('2022-09-05', $periods['dateStart']);
  149. $this->assertEquals('2023-09-04', $periods['dateEnd']);
  150. }
  151. /**
  152. * @see OrganizationUtils::getActivityPeriodsSwitchYear()
  153. */
  154. public function testGetActivityPeriodsSwitchYearNoMusicalDate(): void
  155. {
  156. $organizationUtils = $this->getOrganizationUtilsMockFor('getActivityPeriodsSwitchYear');
  157. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  158. $parameters->method('getMusicalDate')->willReturn(null);
  159. $organization = $this->getMockBuilder(Organization::class)->getMock();
  160. $organization->method('getParameters')->willReturn($parameters);
  161. $periods = $organizationUtils->getActivityPeriodsSwitchYear($organization, 2022);
  162. $this->assertEquals('2022-09-01', $periods['dateStart']);
  163. $this->assertEquals('2023-08-31', $periods['dateEnd']);
  164. }
  165. /**
  166. * @see OrganizationUtils::getActivityYearSwitchDate()
  167. */
  168. public function testGetActivityYearSwitchDate(): void
  169. {
  170. $organizationUtils = $this->getOrganizationUtilsMockFor('getActivityYearSwitchDate');
  171. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  172. $parameters->method('getMusicalDate')->willReturn(new \DateTime('2020-09-05'));
  173. $organization = $this->getMockBuilder(Organization::class)->getMock();
  174. $organization->method('getParameters')->willReturn($parameters);
  175. $this->assertEquals(2022, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-09-10')));
  176. $this->assertEquals(2021, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-09-02')));
  177. }
  178. /**
  179. * @see OrganizationUtils::getActivityYearSwitchDate()
  180. */
  181. public function testGetActivityYearSwitchDateNoMusicalDate(): void
  182. {
  183. $organizationUtils = $this->getOrganizationUtilsMockFor('getActivityYearSwitchDate');
  184. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  185. $parameters->method('getMusicalDate')->willReturn(null);
  186. $organization = $this->getMockBuilder(Organization::class)->getMock();
  187. $organization->method('getParameters')->willReturn($parameters);
  188. $this->assertEquals(2021, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-08-01')));
  189. $this->assertEquals(2022, $organizationUtils->getActivityYearSwitchDate($organization, new \DateTime('2022-09-02')));
  190. }
  191. /**
  192. * @see OrganizationUtils::getOrganizationActiveSubdomain()
  193. */
  194. public function testGetOrganizationActiveSubdomain(): void
  195. {
  196. $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationActiveSubdomain');
  197. $subdomain1 = $this->getMockBuilder(Subdomain::class)->getMock();
  198. $subdomain1->method('isActive')->willReturn(false);
  199. $subdomain2 = $this->getMockBuilder(Subdomain::class)->getMock();
  200. $subdomain2->method('isActive')->willReturn(false);
  201. $subdomain3 = $this->getMockBuilder(Subdomain::class)->getMock();
  202. $subdomain3->method('isActive')->willReturn(true);
  203. $subdomain3->method('getSubdomain')->willReturn('foo');
  204. $organization = $this->getMockBuilder(Organization::class)->getMock();
  205. $organization->method('getSubdomains')->willReturn(new ArrayCollection([$subdomain1, $subdomain2, $subdomain3]));
  206. $this->assertEquals('foo', $organizationUtils->getOrganizationActiveSubdomain($organization));
  207. }
  208. /**
  209. * @see OrganizationUtils::getOrganizationActiveSubdomain()
  210. */
  211. public function testGetOrganizationActiveSubdomainNone(): void
  212. {
  213. $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationActiveSubdomain');
  214. $subdomain1 = $this->getMockBuilder(Subdomain::class)->getMock();
  215. $subdomain1->method('isActive')->willReturn(false);
  216. $organization = $this->getMockBuilder(Organization::class)->getMock();
  217. $organization->method('getSubdomains')->willReturn(new ArrayCollection([$subdomain1]));
  218. $this->assertEquals(null, $organizationUtils->getOrganizationActiveSubdomain($organization));
  219. }
  220. /**
  221. * @see OrganizationUtils::getOrganizationWebsite()
  222. */
  223. public function testOrganizationWebsite(): void
  224. {
  225. $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');
  226. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  227. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  228. $parameters->method('getCustomDomain')->willReturn(null);
  229. $organization = $this->getMockBuilder(Organization::class)->getMock();
  230. $organization->method('getParameters')->willReturn($parameters);
  231. $organizationUtils->method('getOrganizationActiveSubdomain')->with($organization)->willReturn('foo');
  232. $this->assertEquals('https://foo.opentalent.fr', $organizationUtils->getOrganizationWebsite($organization));
  233. }
  234. /**
  235. * @see OrganizationUtils::getOrganizationWebsite()
  236. */
  237. public function testOrganizationWebsiteDeactivatedWithOther(): void
  238. {
  239. $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');
  240. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  241. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  242. $parameters->method('getWebsite')->willReturn('foo.net');
  243. $organization = $this->getMockBuilder(Organization::class)->getMock();
  244. $organization->method('getParameters')->willReturn($parameters);
  245. $this->assertEquals('https://foo.net', $organizationUtils->getOrganizationWebsite($organization));
  246. }
  247. /**
  248. * @see OrganizationUtils::getOrganizationWebsite()
  249. */
  250. public function testOrganizationWebsiteDeactivatedNoOther(): void
  251. {
  252. $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');
  253. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  254. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
  255. $parameters->method('getWebsite')->willReturn(null);
  256. $organization = $this->getMockBuilder(Organization::class)->getMock();
  257. $organization->method('getParameters')->willReturn($parameters);
  258. $this->assertEquals(null, $organizationUtils->getOrganizationWebsite($organization));
  259. }
  260. /**
  261. * @see OrganizationUtils::getOrganizationWebsite()
  262. */
  263. public function testOrganizationWebsiteWithCustom(): void
  264. {
  265. $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');
  266. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  267. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  268. $parameters->method('getCustomDomain')->willReturn('bar.net');
  269. $organization = $this->getMockBuilder(Organization::class)->getMock();
  270. $organization->method('getParameters')->willReturn($parameters);
  271. $this->assertEquals('https://bar.net', $organizationUtils->getOrganizationWebsite($organization));
  272. }
  273. /**
  274. * @see OrganizationUtils::getOrganizationWebsite()
  275. */
  276. public function testOrganizationWebsiteNoSubdomains(): void
  277. {
  278. $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');
  279. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  280. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  281. $parameters->method('getCustomDomain')->willReturn(null);
  282. $organization = $this->getMockBuilder(Organization::class)->getMock();
  283. $organization->method('getParameters')->willReturn($parameters);
  284. $organizationUtils->method('getOrganizationActiveSubdomain')->with($organization)->willReturn(null);
  285. $this->assertEquals(null, $organizationUtils->getOrganizationWebsite($organization));
  286. }
  287. /**
  288. * @see OrganizationUtils::getOrganizationWebsite()
  289. */
  290. public function testOrganizationWebsiteOutOfNetSubdomain(): void
  291. {
  292. $organizationUtils = $this->getOrganizationUtilsMockFor('getOrganizationWebsite');
  293. $parameters = $this->getMockBuilder(Parameters::class)->getMock();
  294. $parameters->method('getDesactivateOpentalentSiteWeb')->willReturn(false);
  295. $parameters->method('getCustomDomain')->willReturn(null);
  296. $organization = $this->getMockBuilder(Organization::class)->getMock();
  297. $organization->method('getParameters')->willReturn($parameters);
  298. $organizationUtils->method('getOrganizationActiveSubdomain')->with($organization)->willReturn('outofnet');
  299. $this->assertEquals('https://opentalent.fr', $organizationUtils->getOrganizationWebsite($organization));
  300. }
  301. /**
  302. * @see Utils::isLastParentAndCMF()
  303. */
  304. public function testIsLastParentAndCMF(): void
  305. {
  306. $organizationUtils = $this->getOrganizationUtilsMockFor('isLastParentAndCMF');
  307. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  308. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  309. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  310. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  311. $this->networkOrganizationRepository
  312. ->method('isLastParent')
  313. ->willReturnMap([
  314. [$organization1, true],
  315. [$organization2, true],
  316. [$organization3, false],
  317. [$organization4, false],
  318. ]);
  319. $this->networkUtils
  320. ->method('isCMF')
  321. ->willReturnMap([
  322. [$organization1, true],
  323. [$organization2, false],
  324. [$organization3, true],
  325. [$organization4, false],
  326. ]);
  327. $this->assertTrue($organizationUtils->isLastParentAndCMF($organization1));
  328. $this->assertFalse($organizationUtils->isLastParentAndCMF($organization2));
  329. $this->assertFalse($organizationUtils->isLastParentAndCMF($organization3));
  330. $this->assertFalse($organizationUtils->isLastParentAndCMF($organization4));
  331. }
  332. /**
  333. * @see Utils::isStructureAndCMF()
  334. */
  335. public function testIsStructureAndCMF(): void
  336. {
  337. $organizationUtils = $this->getOrganizationUtilsMockFor('isStructureAndCMF');
  338. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  339. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  340. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  341. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  342. $organizationUtils
  343. ->method('isStructure')
  344. ->willReturnMap([
  345. [$organization1, true],
  346. [$organization2, false],
  347. [$organization3, true],
  348. [$organization4, false],
  349. ]);
  350. $this->networkUtils
  351. ->method('isCMF')
  352. ->with($organization1)
  353. ->willReturnMap([
  354. [$organization1, true],
  355. [$organization2, false],
  356. [$organization3, false],
  357. [$organization4, true],
  358. ]);
  359. $this->assertTrue($organizationUtils->isStructureAndCMF($organization1));
  360. $this->assertFalse($organizationUtils->isStructureAndCMF($organization2));
  361. $this->assertFalse($organizationUtils->isStructureAndCMF($organization3));
  362. $this->assertFalse($organizationUtils->isStructureAndCMF($organization4));
  363. }
  364. /**
  365. * @see Utils::isManagerAndCMF()
  366. */
  367. public function testIsManagerAndCMF(): void
  368. {
  369. $organizationUtils = $this->getOrganizationUtilsMockFor('isManagerAndCMF');
  370. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  371. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  372. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  373. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  374. $organizationUtils
  375. ->method('isManager')
  376. ->willReturnMap([
  377. [$organization1, true],
  378. [$organization2, false],
  379. [$organization3, true],
  380. [$organization4, false],
  381. ]);
  382. $this->networkUtils
  383. ->method('isCMF')
  384. ->willReturnMap([
  385. [$organization1, true],
  386. [$organization2, false],
  387. [$organization3, false],
  388. [$organization4, true],
  389. ]);
  390. $this->assertTrue($organizationUtils->isManagerAndCMF($organization1));
  391. $this->assertFalse($organizationUtils->isManagerAndCMF($organization2));
  392. $this->assertFalse($organizationUtils->isManagerAndCMF($organization3));
  393. $this->assertFalse($organizationUtils->isManagerAndCMF($organization4));
  394. }
  395. /**
  396. * @see Utils::isManagerAndNotLastParentAndCMF()
  397. */
  398. public function testIsManagerAndLastParentAndCMF(): void
  399. {
  400. $organizationUtils = $this->getOrganizationUtilsMockFor('isManagerAndLastParentAndCMF');
  401. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  402. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  403. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  404. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  405. $organizationUtils
  406. ->method('isManager')
  407. ->willReturnMap([
  408. [$organization1, true],
  409. [$organization2, false],
  410. [$organization3, true],
  411. [$organization4, false],
  412. ]);
  413. $organizationUtils
  414. ->method('isLastParentAndCMF')
  415. ->willReturnMap([
  416. [$organization1, true],
  417. [$organization2, false],
  418. [$organization3, false],
  419. [$organization4, true],
  420. ]);
  421. $this->assertTrue($organizationUtils->isManagerAndLastParentAndCMF($organization1));
  422. $this->assertFalse($organizationUtils->isManagerAndLastParentAndCMF($organization2));
  423. $this->assertFalse($organizationUtils->isManagerAndLastParentAndCMF($organization3));
  424. $this->assertFalse($organizationUtils->isManagerAndLastParentAndCMF($organization4));
  425. }
  426. /**
  427. * @see Utils::isManagerAndNotLastParentAndCMF()
  428. */
  429. public function testIsManagerAndNotLastParentAndCMF(): void
  430. {
  431. $organizationUtils = $this->getOrganizationUtilsMockFor('isManagerAndNotLastParentAndCMF');
  432. $organization1 = $this->getMockBuilder(Organization::class)->getMock();
  433. $organization2 = $this->getMockBuilder(Organization::class)->getMock();
  434. $organization3 = $this->getMockBuilder(Organization::class)->getMock();
  435. $organization4 = $this->getMockBuilder(Organization::class)->getMock();
  436. $organizationUtils
  437. ->method('isManager')
  438. ->willReturnMap([
  439. [$organization1, true],
  440. [$organization2, false],
  441. [$organization3, true],
  442. [$organization4, true],
  443. ]);
  444. $this->networkOrganizationRepository
  445. ->method('isLastParent')
  446. ->willReturnMap([
  447. [$organization1, false],
  448. [$organization2, false],
  449. [$organization3, true],
  450. [$organization4, false],
  451. ]);
  452. $this->networkUtils
  453. ->method('isCMF')
  454. ->willReturnMap([
  455. [$organization1, true],
  456. [$organization2, true],
  457. [$organization3, true],
  458. [$organization4, false],
  459. ]);
  460. $this->assertTrue($organizationUtils->isManagerAndNotLastParentAndCMF($organization1));
  461. $this->assertFalse($organizationUtils->isManagerAndNotLastParentAndCMF($organization2));
  462. $this->assertFalse($organizationUtils->isManagerAndNotLastParentAndCMF($organization3));
  463. $this->assertFalse($organizationUtils->isManagerAndNotLastParentAndCMF($organization4));
  464. }
  465. /**
  466. * @see OrganizationUtils::isSchool()
  467. */
  468. public function testIsSchool(): void
  469. {
  470. $organizationUtils = $this->getOrganizationUtilsMockFor('isSchool');
  471. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
  472. $this->assertFalse($organizationUtils->isSchool($organization));
  473. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
  474. $this->assertFalse($organizationUtils->isSchool($organization));
  475. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
  476. $this->assertTrue($organizationUtils->isSchool($organization));
  477. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
  478. $this->assertTrue($organizationUtils->isSchool($organization));
  479. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
  480. $this->assertFalse($organizationUtils->isSchool($organization));
  481. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
  482. $this->assertFalse($organizationUtils->isSchool($organization));
  483. }
  484. /**
  485. * @see OrganizationUtils::isArtist()
  486. */
  487. public function testIsArtist(): void
  488. {
  489. $organizationUtils = $this->getOrganizationUtilsMockFor('isArtist');
  490. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST);
  491. $this->assertTrue($organizationUtils->isArtist($organization));
  492. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::ARTIST_PREMIUM);
  493. $this->assertTrue($organizationUtils->isArtist($organization));
  494. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL);
  495. $this->assertFalse($organizationUtils->isArtist($organization));
  496. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::SCHOOL_PREMIUM);
  497. $this->assertFalse($organizationUtils->isArtist($organization));
  498. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER);
  499. $this->assertFalse($organizationUtils->isArtist($organization));
  500. $organization = $this->createOrganizationWithProductMock(SettingsProductEnum::MANAGER_PREMIUM);
  501. $this->assertFalse($organizationUtils->isArtist($organization));
  502. }
  503. /**
  504. * @see OrganizationUtils::hasModule()
  505. */
  506. public function testHasModule(): void
  507. {
  508. $organizationUtils = $this->getOrganizationUtilsMockFor('hasModule');
  509. $settings = $this->getMockBuilder(Settings::class)->getMock();
  510. $settings->method('getModules')->willReturn(['foo', 'bar']);
  511. $organization = $this->getMockBuilder(Organization::class)->getMock();
  512. $organization->method('getSettings')->willReturn($settings);
  513. $this->assertTrue($organizationUtils->hasModule($organization, 'foo'));
  514. $this->assertFalse($organizationUtils->hasModule($organization, 'other'));
  515. }
  516. /**
  517. * @see OrganizationUtils::getActiveNetworkOrganization()
  518. */
  519. public function testGetActiveNetworkOrganization(): void
  520. {
  521. $organizationUtils = $this->getOrganizationUtilsMockFor('getActiveNetworkOrganization');
  522. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  523. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  524. $networkOrganization3 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  525. $this
  526. ->networkUtils
  527. ->expects(self::exactly(2))
  528. ->method('isNetworkOrganizationActiveNow')
  529. ->willReturnMap([
  530. [$networkOrganization1, false],
  531. [$networkOrganization2, true],
  532. [$networkOrganization3, false],
  533. ]);
  534. $organization = $this->getMockBuilder(Organization::class)->getMock();
  535. $organization
  536. ->method('getNetworkOrganizations')
  537. ->willReturn(new ArrayCollection([$networkOrganization1, $networkOrganization2, $networkOrganization3]));
  538. $result = $organizationUtils->getActiveNetworkOrganization($organization);
  539. $this->assertEquals($networkOrganization2, $result);
  540. }
  541. /**
  542. * @see OrganizationUtils::getActiveNetworkOrganization()
  543. */
  544. public function testGetActiveNetworkOrganizationNoActive(): void
  545. {
  546. $organizationUtils = $this->getOrganizationUtilsMockFor('getActiveNetworkOrganization');
  547. $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  548. $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
  549. $this
  550. ->networkUtils
  551. ->expects(self::exactly(2))
  552. ->method('isNetworkOrganizationActiveNow')
  553. ->willReturnMap([
  554. [$networkOrganization1, false],
  555. [$networkOrganization2, false],
  556. ]);
  557. $organization = $this->getMockBuilder(Organization::class)->getMock();
  558. $organization
  559. ->method('getNetworkOrganizations')
  560. ->willReturn(new ArrayCollection([$networkOrganization1, $networkOrganization2]));
  561. $result = $organizationUtils->getActiveNetworkOrganization($organization);
  562. $this->assertEquals(null, $result);
  563. }
  564. }