DolibarrUtilsTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Service\Dolibarr;
  4. use App\Enum\Organization\SettingsProductEnum;
  5. use App\Service\Dolibarr\DolibarrUtils;
  6. use App\Service\Utils\DatesUtils;
  7. use Doctrine\DBAL\Connection;
  8. use PHPUnit\Framework\MockObject\MockObject;
  9. use PHPUnit\Framework\TestCase;
  10. /**
  11. * Class to expose protected methods for testing.
  12. */
  13. class TestableDolibarrUtils extends DolibarrUtils
  14. {
  15. public function executeQueryPublic(string $sql): void
  16. {
  17. $this->executeQuery($sql, []);
  18. }
  19. }
  20. class DolibarrUtilsTest extends TestCase
  21. {
  22. private Connection|MockObject $dolibarrConnection;
  23. private TestableDolibarrUtils $dolibarrUtils;
  24. protected function setUp(): void
  25. {
  26. $this->dolibarrConnection = $this->createMock(Connection::class);
  27. $this->dolibarrUtils = new TestableDolibarrUtils($this->dolibarrConnection);
  28. }
  29. /**
  30. * @see DolibarrUtils::getProductId()
  31. */
  32. public function testGetProductIdForArtistPremiumTrial(): void
  33. {
  34. $result = $this->dolibarrUtils->getProductId(
  35. SettingsProductEnum::ARTIST_PREMIUM,
  36. true,
  37. false
  38. );
  39. $this->assertEquals(DolibarrUtils::ARTIST_PREMIUM_TRIAL_PRODUCT_ID, $result);
  40. }
  41. /**
  42. * @see DolibarrUtils::getProductId()
  43. */
  44. public function testGetProductIdForArtistPremiumCmf(): void
  45. {
  46. $result = $this->dolibarrUtils->getProductId(
  47. SettingsProductEnum::ARTIST_PREMIUM,
  48. false,
  49. true
  50. );
  51. $this->assertEquals(DolibarrUtils::ARTIST_PREMIUM_CMF_PRODUCT_ID, $result);
  52. }
  53. /**
  54. * @see DolibarrUtils::getProductId()
  55. */
  56. public function testGetProductIdForArtistPremium(): void
  57. {
  58. $result = $this->dolibarrUtils->getProductId(
  59. SettingsProductEnum::ARTIST_PREMIUM,
  60. false,
  61. false
  62. );
  63. $this->assertEquals(DolibarrUtils::ARTIST_PREMIUM_PRODUCT_ID, $result);
  64. }
  65. /**
  66. * @see DolibarrUtils::getProductId()
  67. */
  68. public function testGetProductIdForArtistCmf(): void
  69. {
  70. $result = $this->dolibarrUtils->getProductId(
  71. SettingsProductEnum::ARTIST,
  72. false,
  73. true
  74. );
  75. $this->assertEquals(DolibarrUtils::ARTIST_STANDARD_CMF_PRODUCT_ID, $result);
  76. }
  77. /**
  78. * @see DolibarrUtils::getProductId()
  79. */
  80. public function testGetProductIdWithInvalidContractType(): void
  81. {
  82. $this->expectException(\InvalidArgumentException::class);
  83. $this->expectExceptionMessage('Invalid contract type');
  84. $this->dolibarrUtils->getProductId(
  85. SettingsProductEnum::SCHOOL,
  86. false,
  87. false
  88. );
  89. }
  90. /**
  91. * @see DolibarrUtils::executeQuery()
  92. */
  93. public function testExecuteQuery(): void
  94. {
  95. $sql = 'DELETE FROM llx_societe_commerciaux WHERE fk_soc = 123';
  96. $this->dolibarrConnection
  97. ->expects($this->once())
  98. ->method('executeQuery')
  99. ->with($sql, []);
  100. $this->dolibarrUtils->executeQueryPublic($sql);
  101. }
  102. /**
  103. * @see DolibarrUtils::updateSocietyCommercialsWithApi()
  104. */
  105. public function testUpdateSocietyCommercialsWithApi(): void
  106. {
  107. $societyId = 123;
  108. $apiUserId = 8;
  109. $this->dolibarrConnection
  110. ->expects($this->exactly(2))
  111. ->method('executeQuery')
  112. ->withConsecutive(
  113. ['DELETE FROM llx_societe_commerciaux WHERE fk_soc = ?', [$societyId]],
  114. ['INSERT INTO llx_societe_commerciaux (fk_soc, fk_user) VALUES (?, ?)', [$societyId, $apiUserId]]
  115. );
  116. $this->dolibarrUtils->updateSocietyCommercialsWithApi($societyId);
  117. }
  118. /**
  119. * @see DolibarrUtils::addActionComm()
  120. */
  121. public function testAddActionComm(): void
  122. {
  123. $societyId = 123;
  124. $title = 'Test Title';
  125. $message = 'Test Message';
  126. $apiUserId = 8;
  127. // Mock DatesUtils to return a fixed date
  128. $tz = new \DateTimeZone('Europe/Paris');
  129. $mockDate = new \DateTime('2023-01-01 12:00:00', $tz);
  130. $formattedDate = $mockDate->format('Y-m-d H:i:s');
  131. // Create a partial mock of DatesUtils to control the 'new' static method
  132. $datesMock = $this->createMock(DatesUtils::class);
  133. DatesUtils::setFakeDatetime('2023-01-01 12:00:00');
  134. $sql = "INSERT INTO llx_actioncomm (fk_soc, ref, code, label, note, datep, datep2, datec, fk_user_author, fk_user_mod, fk_user_action, percent)
  135. VALUES (?, -1, 'AC_OT_ONLINE_STORE', ?, ?, ?, ?, ?, ?, ?, ?, -1)";
  136. $this->dolibarrConnection
  137. ->expects($this->once())
  138. ->method('executeQuery')
  139. ->with(
  140. $sql,
  141. [
  142. $societyId,
  143. $title,
  144. $message,
  145. $formattedDate,
  146. $formattedDate,
  147. $formattedDate,
  148. $apiUserId,
  149. $apiUserId,
  150. $apiUserId,
  151. ]
  152. );
  153. $this->dolibarrUtils->addActionComm($societyId, $title, $message);
  154. }
  155. /**
  156. * @see DolibarrUtils::getDolibarrProductName()
  157. */
  158. public function testGetDolibarrProductNameForArtist(): void
  159. {
  160. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::ARTIST);
  161. $this->assertEquals('Opentalent Artist', $result);
  162. }
  163. /**
  164. * @see DolibarrUtils::getDolibarrProductName()
  165. */
  166. public function testGetDolibarrProductNameForArtistPremiumTrial(): void
  167. {
  168. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::ARTIST_PREMIUM, true);
  169. $this->assertEquals('Opentalent Artist Premium (Essai)', $result);
  170. }
  171. /**
  172. * @see DolibarrUtils::getDolibarrProductName()
  173. */
  174. public function testGetDolibarrProductNameForArtistPremium(): void
  175. {
  176. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::ARTIST_PREMIUM);
  177. $this->assertEquals('Opentalent Artist Premium', $result);
  178. }
  179. /**
  180. * @see DolibarrUtils::getDolibarrProductName()
  181. */
  182. public function testGetDolibarrProductNameForSchool(): void
  183. {
  184. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::SCHOOL);
  185. $this->assertEquals('Opentalent School', $result);
  186. }
  187. /**
  188. * @see DolibarrUtils::getDolibarrProductName()
  189. */
  190. public function testGetDolibarrProductNameForSchoolPremiumTrial(): void
  191. {
  192. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::SCHOOL_PREMIUM, true);
  193. $this->assertEquals('Opentalent School Premium (Essai)', $result);
  194. }
  195. /**
  196. * @see DolibarrUtils::getDolibarrProductName()
  197. */
  198. public function testGetDolibarrProductNameForSchoolPremium(): void
  199. {
  200. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::SCHOOL_PREMIUM);
  201. $this->assertEquals('Opentalent School Premium', $result);
  202. }
  203. /**
  204. * @see DolibarrUtils::getDolibarrProductName()
  205. */
  206. public function testGetDolibarrProductNameForManager(): void
  207. {
  208. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::MANAGER);
  209. $this->assertEquals('Opentalent Manager', $result);
  210. }
  211. /**
  212. * @see DolibarrUtils::getDolibarrProductName()
  213. */
  214. public function testGetDolibarrProductNameForManagerPremium(): void
  215. {
  216. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::MANAGER_PREMIUM);
  217. $this->assertEquals('Opentalent Manager Premium', $result);
  218. }
  219. }