/tests/wpunit/DownloadableItemQueriesTest.php

https://github.com/wp-graphql/wp-graphql-woocommerce · PHP · 396 lines · 315 code · 37 blank · 44 comment · 0 complexity · d3065b0637bdf296ea0817303acbbbe9 MD5 · raw file

  1. <?php
  2. class DownloadableItemQueriesTest extends \Codeception\TestCase\WPTestCase
  3. {
  4. public function setUp() {
  5. // before
  6. parent::setUp();
  7. // your set up methods here
  8. $this->customers = $this->getModule('\Helper\Wpunit')->customer();
  9. $this->orders = $this->getModule('\Helper\Wpunit')->order();
  10. $this->products = $this->getModule('\Helper\Wpunit')->product();
  11. update_option( 'woocommerce_downloads_grant_access_after_payment', 'yes' );
  12. $this->customer = $this->customers->create();
  13. }
  14. public function tearDown() {
  15. // your tear down methods here
  16. // then
  17. parent::tearDown();
  18. }
  19. public function set_user( $user ) {
  20. wp_set_current_user( $user );
  21. WC()->customer = new WC_Customer( get_current_user_id(), true );
  22. }
  23. // tests
  24. public function testOrderToDownloadableItemsQuery() {
  25. $downloadable_product = $this->products->create_simple(
  26. array(
  27. 'downloadable' => true,
  28. 'downloads' => array( $this->products->create_download() )
  29. )
  30. );
  31. $order_id = $this->orders->create(
  32. array(
  33. 'status' => 'completed',
  34. 'customer_id' => $this->customer,
  35. ),
  36. array(
  37. 'line_items' => array(
  38. array(
  39. 'product' => $downloadable_product,
  40. 'qty' => 1,
  41. ),
  42. ),
  43. )
  44. );
  45. // Force download permission updated.
  46. wc_downloadable_product_permissions( $order_id, true );
  47. $query = '
  48. query {
  49. customer {
  50. orders {
  51. nodes {
  52. downloadableItems(first: 1) {
  53. nodes {
  54. url
  55. accessExpires
  56. downloadId
  57. downloadsRemaining
  58. name
  59. product {
  60. productId
  61. }
  62. download {
  63. downloadId
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. ';
  72. /**
  73. * Assertion One
  74. *
  75. * tests query results
  76. */
  77. $this->set_user( $this->customer );
  78. $actual = graphql( array( 'query' => $query ) );
  79. $expected = array(
  80. 'data' => array(
  81. 'customer' => array(
  82. 'orders' => array(
  83. 'nodes' => array(
  84. array(
  85. 'downloadableItems' => array( 'nodes' => $this->orders->print_downloadables( $order_id ) ),
  86. ),
  87. ),
  88. ),
  89. ),
  90. ),
  91. );
  92. // use --debug flag to view.
  93. codecept_debug( $actual );
  94. $this->assertEquals( $expected, $actual );
  95. }
  96. public function testOrderToDownloadableItemsQueryArgs() {
  97. $valid_product = $this->products->create_simple(
  98. array(
  99. 'downloadable' => true,
  100. 'downloads' => array( $this->products->create_download() )
  101. )
  102. );
  103. $downloadable_product = $this->products->create_simple(
  104. array(
  105. 'download_expiry' => 5,
  106. 'download_limit' => 3,
  107. 'downloadable' => true,
  108. 'downloads' => array( $this->products->create_download() )
  109. )
  110. );
  111. $downloaded_product = $this->products->create_simple(
  112. array(
  113. 'download_limit' => 0,
  114. 'downloadable' => true,
  115. 'downloads' => array( $this->products->create_download() )
  116. )
  117. );
  118. $order_id = $this->orders->create(
  119. array(
  120. 'status' => 'completed',
  121. 'customer_id' => $this->customer,
  122. ),
  123. array(
  124. 'line_items' => array(
  125. array(
  126. 'product' => $valid_product,
  127. 'qty' => 1,
  128. ),
  129. array(
  130. 'product' => $downloadable_product,
  131. 'qty' => 1,
  132. ),
  133. array(
  134. 'product' => $downloaded_product,
  135. 'qty' => 1,
  136. ),
  137. ),
  138. )
  139. );
  140. // Force download permission updated.
  141. wc_downloadable_product_permissions( $order_id, true );
  142. $query = '
  143. query($input: OrderToDownloadableItemConnectionWhereArgs) {
  144. customer {
  145. orders {
  146. nodes {
  147. downloadableItems(where: $input) {
  148. nodes {
  149. product {
  150. productId
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. ';
  159. /**
  160. * Assertion One
  161. *
  162. * tests "active" whereArg
  163. */
  164. $this->set_user( $this->customer );
  165. $actual = graphql(
  166. array(
  167. 'query' => $query,
  168. 'variables' => array( 'input' => array( 'active' => true ) ),
  169. )
  170. );
  171. $expected = array(
  172. 'data' => array(
  173. 'customer' => array(
  174. 'orders' => array(
  175. 'nodes' => array(
  176. array(
  177. 'downloadableItems' => array(
  178. 'nodes' => array_map(
  179. function( $product_id ) {
  180. return array( 'product' => array( 'productId' => $product_id ) );
  181. },
  182. array( $valid_product, $downloadable_product )
  183. ),
  184. ),
  185. ),
  186. ),
  187. ),
  188. ),
  189. ),
  190. );
  191. // use --debug flag to view.
  192. codecept_debug( $actual );
  193. $this->assertEquals( $expected, $actual );
  194. /**
  195. * Assertion Two
  196. *
  197. * tests "active" whereArg reversed
  198. */
  199. $actual = graphql(
  200. array(
  201. 'query' => $query,
  202. 'variables' => array( 'input' => array( 'active' => false ) ),
  203. )
  204. );
  205. $expected = array(
  206. 'data' => array(
  207. 'customer' => array(
  208. 'orders' => array(
  209. 'nodes' => array(
  210. array(
  211. 'downloadableItems' => array(
  212. 'nodes' => array_map(
  213. function( $product_id ) {
  214. return array( 'product' => array( 'productId' => $product_id ) );
  215. },
  216. array( $downloaded_product )
  217. ),
  218. ),
  219. ),
  220. ),
  221. ),
  222. ),
  223. ),
  224. );
  225. // use --debug flag to view.
  226. codecept_debug( $actual );
  227. $this->assertEquals( $expected, $actual );
  228. /**
  229. * Assertion Three
  230. *
  231. * tests "hasDownloadsRemaining" whereArg
  232. */
  233. $actual = graphql(
  234. array(
  235. 'query' => $query,
  236. 'variables' => array( 'input' => array( 'hasDownloadsRemaining' => true ) )
  237. )
  238. );
  239. $expected = array(
  240. 'data' => array(
  241. 'customer' => array(
  242. 'orders' => array(
  243. 'nodes' => array(
  244. array(
  245. 'downloadableItems' => array(
  246. 'nodes' => array_map(
  247. function( $product_id ) {
  248. return array( 'product' => array( 'productId' => $product_id ) );
  249. },
  250. array( $valid_product, $downloadable_product )
  251. ),
  252. ),
  253. ),
  254. ),
  255. ),
  256. ),
  257. ),
  258. );
  259. // use --debug flag to view.
  260. codecept_debug( $actual );
  261. $this->assertEquals( $expected, $actual );
  262. /**
  263. * Assertion Four
  264. *
  265. * tests "hasDownloadsRemaining" whereArg reversed
  266. */
  267. $actual = graphql(
  268. array(
  269. 'query' => $query,
  270. 'variables' => array( 'input' => array( 'hasDownloadsRemaining' => false ) )
  271. )
  272. );
  273. $expected = array(
  274. 'data' => array(
  275. 'customer' => array(
  276. 'orders' => array(
  277. 'nodes' => array(
  278. array(
  279. 'downloadableItems' => array(
  280. 'nodes' => array_map(
  281. function( $product_id ) {
  282. return array( 'product' => array( 'productId' => $product_id ) );
  283. },
  284. array( $downloaded_product )
  285. ),
  286. ),
  287. ),
  288. ),
  289. ),
  290. ),
  291. ),
  292. );
  293. // use --debug flag to view.
  294. codecept_debug( $actual );
  295. $this->assertEquals( $expected, $actual );
  296. }
  297. public function testCustomerToDownloadableItemsQuery() {
  298. $downloadable_product = $this->products->create_simple(
  299. array(
  300. 'downloadable' => true,
  301. 'downloads' => array( $this->products->create_download() )
  302. )
  303. );
  304. $order_id = $this->orders->create(
  305. array(
  306. 'status' => 'completed',
  307. 'customer_id' => $this->customer,
  308. ),
  309. array(
  310. 'line_items' => array(
  311. array(
  312. 'product' => $downloadable_product,
  313. 'qty' => 1,
  314. ),
  315. ),
  316. )
  317. );
  318. // Force download permission updated.
  319. wc_downloadable_product_permissions( $order_id, true );
  320. $query = '
  321. query {
  322. customer {
  323. downloadableItems(first: 1) {
  324. nodes {
  325. url
  326. accessExpires
  327. downloadId
  328. downloadsRemaining
  329. name
  330. product {
  331. productId
  332. }
  333. download {
  334. downloadId
  335. }
  336. }
  337. }
  338. }
  339. }
  340. ';
  341. /**
  342. * Assertion One
  343. *
  344. * tests query results
  345. */
  346. $this->set_user( $this->customer );
  347. $actual = graphql( array( 'query' => $query ) );
  348. $expected = array(
  349. 'data' => array(
  350. 'customer' => array(
  351. 'downloadableItems' => array( 'nodes' => $this->customers->print_downloadables( $this->customer ) ),
  352. ),
  353. ),
  354. );
  355. // use --debug flag to view.
  356. codecept_debug( $actual );
  357. $this->assertEquals( $expected, $actual );
  358. }
  359. }