DolibarrUtilsTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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)
  115. VALUES ($societyId, $apiUserId)"]
  116. );
  117. $this->dolibarrUtils->updateSocietyCommercialsWithApi($societyId);
  118. }
  119. /**
  120. * @see DolibarrUtils::addActionComm()
  121. */
  122. public function testAddActionComm(): void
  123. {
  124. $societyId = 123;
  125. $title = 'Test Title';
  126. $message = 'Test Message';
  127. $apiUserId = 8;
  128. // Mock DatesUtils to return a fixed date
  129. $tz = new \DateTimeZone('Europe/Paris');
  130. $mockDate = new \DateTime('2023-01-01 12:00:00', $tz);
  131. $formattedDate = $mockDate->format('Y-m-d H:i:s');
  132. // Create a partial mock of DatesUtils to control the 'new' static method
  133. $datesMock = $this->createMock(DatesUtils::class);
  134. DatesUtils::setFakeDatetime('2023-01-01 12:00:00');
  135. $this->dolibarrConnection
  136. ->expects($this->once())
  137. ->method('executeQuery')
  138. ->with(
  139. "INSERT INTO llx_actioncomm (fk_soc, ref, code, label, note, datep, datep2, datec, fk_user_author, fk_user_mod, fk_user_action, percent)
  140. VALUES ($societyId, -1, 'AC_OT_ONLINE_STORE', '$title', '$message', '$formattedDate', '$formattedDate', '$formattedDate', $apiUserId, $apiUserId, $apiUserId, -1)"
  141. );
  142. $this->dolibarrUtils->addActionComm($societyId, $title, $message);
  143. }
  144. /**
  145. * @see DolibarrUtils::getDolibarrProductName()
  146. */
  147. public function testGetDolibarrProductNameForArtist(): void
  148. {
  149. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::ARTIST);
  150. $this->assertEquals('Opentalent Artist', $result);
  151. }
  152. /**
  153. * @see DolibarrUtils::getDolibarrProductName()
  154. */
  155. public function testGetDolibarrProductNameForArtistPremiumTrial(): void
  156. {
  157. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::ARTIST_PREMIUM, true);
  158. $this->assertEquals('Opentalent Artist Premium (Essai)', $result);
  159. }
  160. /**
  161. * @see DolibarrUtils::getDolibarrProductName()
  162. */
  163. public function testGetDolibarrProductNameForArtistPremium(): void
  164. {
  165. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::ARTIST_PREMIUM);
  166. $this->assertEquals('Opentalent Artist Premium', $result);
  167. }
  168. /**
  169. * @see DolibarrUtils::getDolibarrProductName()
  170. */
  171. public function testGetDolibarrProductNameForSchool(): void
  172. {
  173. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::SCHOOL);
  174. $this->assertEquals('Opentalent School', $result);
  175. }
  176. /**
  177. * @see DolibarrUtils::getDolibarrProductName()
  178. */
  179. public function testGetDolibarrProductNameForSchoolPremiumTrial(): void
  180. {
  181. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::SCHOOL_PREMIUM, true);
  182. $this->assertEquals('Opentalent School Premium (Essai)', $result);
  183. }
  184. /**
  185. * @see DolibarrUtils::getDolibarrProductName()
  186. */
  187. public function testGetDolibarrProductNameForSchoolPremium(): void
  188. {
  189. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::SCHOOL_PREMIUM);
  190. $this->assertEquals('Opentalent School Premium', $result);
  191. }
  192. /**
  193. * @see DolibarrUtils::getDolibarrProductName()
  194. */
  195. public function testGetDolibarrProductNameForManager(): void
  196. {
  197. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::MANAGER);
  198. $this->assertEquals('Opentalent Manager', $result);
  199. }
  200. /**
  201. * @see DolibarrUtils::getDolibarrProductName()
  202. */
  203. public function testGetDolibarrProductNameForManagerPremium(): void
  204. {
  205. $result = $this->dolibarrUtils->getDolibarrProductName(SettingsProductEnum::MANAGER_PREMIUM);
  206. $this->assertEquals('Opentalent Manager Premium', $result);
  207. }
  208. }