diff --git a/src/include/class-wc-retailcrm-upload-discount-price.php b/src/include/class-wc-retailcrm-upload-discount-price.php index c6973d6..c36101d 100644 --- a/src/include/class-wc-retailcrm-upload-discount-price.php +++ b/src/include/class-wc-retailcrm-upload-discount-price.php @@ -58,7 +58,12 @@ if (!class_exists('WC_Retailcrm_Upload_Discount_Price')): ] ); - wp_cache_flush_runtime(); + /** WP version >= 6 */ + if (function_exists('wp_cache_flush_runtime')) { + wp_cache_flush_runtime(); + } else { + wp_cache_flush(); + } if (empty($products)) { writeBaseLogs('Can`t get products!'); @@ -147,7 +152,9 @@ if (!class_exists('WC_Retailcrm_Upload_Discount_Price')): private function uploadSettings() { - if (!$this->apiClient instanceof WC_Retailcrm_Proxy) { + if (!$this->apiClient instanceof WC_Retailcrm_Proxy + && !$this->apiClient instanceof WC_Retailcrm_Client_V5 + ) { return 'API client has not been initialized'; } diff --git a/tests/datasets/data-upload-price-retailcrm.php b/tests/datasets/data-upload-price-retailcrm.php new file mode 100644 index 0000000..3a5198c --- /dev/null +++ b/tests/datasets/data-upload-price-retailcrm.php @@ -0,0 +1,56 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ +class DataUploadPriceRetailCrm +{ + public static function dataGetPriceTypes() { + return [ + 'success' => true, + 'priceTypes' => [ + [ + 'code' => 'test', + 'name' => 'test', + 'active' => true, + 'description' => 'test', + 'ordering' => 999, + 'promo' => true, + 'default' => false + ], + [ + 'code' => 'default', + 'name' => 'default', + 'active' => true, + 'description' => 'default', + 'ordering' => 999, + 'promo' => true, + 'default' => true + ], + ] + ]; + } + + public static function willSendPriceType() { + return [ + 'code' => 'woo-promotion-lp', + 'name' => 'Woocommerce promotional price', + 'active' => true, + 'description' => 'Promotional price type for Woocommerce store, generated automatically. + Necessary for correct synchronization work when loyalty program is enabled + (Do not delete. Do not deactivate)', + 'ordering' => 999, + 'promo' => true, + ]; + } +} diff --git a/tests/test-wc-retailcrm-upload-discount-price.php b/tests/test-wc-retailcrm-upload-discount-price.php new file mode 100644 index 0000000..0d3894c --- /dev/null +++ b/tests/test-wc-retailcrm-upload-discount-price.php @@ -0,0 +1,74 @@ + + * @license http://retailcrm.ru Proprietary + * @link http://retailcrm.ru + * @see http://help.retailcrm.ru + */ +class WC_Retailcrm_Upload_Discount_Price_Test extends WC_Retailcrm_Test_Case_Helper +{ + protected $apiMock; + protected $responseMock; + + public function setUp() + { + WC_Helper_Product::create_simple_product(); + WC_Helper_Product::create_variation_product(); + + $this->setOptions(); + + $this->responseMock = $this->getMockBuilder('\WC_Retailcrm_Response_Helper') + ->disableOriginalConstructor() + ->setMethods(['isSuccessful']) + ->getMock() + ; + + $this->apiMock = $this->getMockBuilder('\WC_Retailcrm_Client_V5') + ->disableOriginalConstructor() + ->setMethods(['storePricesUpload', 'getSingleSiteForKey', 'getPriceTypes', 'editPriceType']) + ->getMock() + ; + + $this->responseMock->setResponse(['success' => true]); + $this->setMockResponse($this->responseMock, 'isSuccessful', true); + $this->setMockResponse($this->apiMock, 'getSingleSiteForKey', 'woo'); + + $this->responseMock->setResponse(DataUploadPriceRetailCrm::dataGetPriceTypes()); + $this->setMockResponse($this->apiMock, 'getPriceTypes', $this->responseMock); + } + + public function testUpload() + { + $this->apiMock + ->expects($this->exactly(1)) + ->method('storePricesUpload') + ->with($this->callback( + function ($parameter) { + if (is_array($parameter)) { + return true; + } + + return false; + } + ), $this->equalTo('woo')) + ; + + $this->apiMock + ->expects($this->exactly(1)) + ->method('editPriceType') + ->with($this->identicalTo(DataUploadPriceRetailCrm::willSendPriceType())) + ->willReturn($this->responseMock) + ; + + $uploadService = new WC_Retailcrm_Upload_Discount_Price($this->apiMock); + $uploadService->upload(); + } +}