QueryComplexityLimiterTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Framework;
  8. use Magento\TestFramework\TestCase\GraphQlAbstract;
  9. /**
  10. * Tests query complexity limiter and depth limiter.
  11. * Actual for production mode only
  12. */
  13. class QueryComplexityLimiterTest extends GraphQlAbstract
  14. {
  15. /**
  16. * @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
  17. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  18. */
  19. public function testQueryComplexityIsLimited()
  20. {
  21. $query
  22. = <<<QUERY
  23. {
  24. category(id: 2) {
  25. products {
  26. items {
  27. name
  28. categories {
  29. id
  30. position
  31. level
  32. url_key
  33. url_path
  34. product_count
  35. breadcrumbs {
  36. category_id
  37. category_name
  38. category_url_key
  39. }
  40. products {
  41. items {
  42. media_gallery_entries {
  43. file
  44. }
  45. name
  46. special_from_date
  47. special_to_date
  48. new_to_date
  49. new_from_date
  50. tier_price
  51. manufacturer
  52. thumbnail {
  53. url
  54. label
  55. }
  56. sku
  57. image {
  58. url
  59. label
  60. }
  61. canonical_url
  62. updated_at
  63. created_at
  64. categories {
  65. id
  66. position
  67. level
  68. url_key
  69. url_path
  70. product_count
  71. breadcrumbs {
  72. category_id
  73. category_name
  74. category_url_key
  75. }
  76. products {
  77. items {
  78. name
  79. special_from_date
  80. special_to_date
  81. new_to_date
  82. thumbnail {
  83. url
  84. label
  85. }
  86. new_from_date
  87. tier_price
  88. manufacturer
  89. sku
  90. image {
  91. url
  92. label
  93. }
  94. canonical_url
  95. updated_at
  96. created_at
  97. media_gallery_entries {
  98. position
  99. id
  100. types
  101. }
  102. categories {
  103. id
  104. position
  105. level
  106. url_key
  107. url_path
  108. product_count
  109. breadcrumbs {
  110. category_id
  111. category_name
  112. category_url_key
  113. }
  114. products {
  115. items {
  116. name
  117. special_from_date
  118. special_to_date
  119. new_to_date
  120. new_from_date
  121. tier_price
  122. manufacturer
  123. thumbnail {
  124. url
  125. label
  126. }
  127. sku
  128. image {
  129. url
  130. label
  131. }
  132. canonical_url
  133. updated_at
  134. created_at
  135. categories {
  136. id
  137. position
  138. level
  139. url_key
  140. url_path
  141. product_count
  142. breadcrumbs {
  143. category_id
  144. category_name
  145. category_url_key
  146. }
  147. products {
  148. items {
  149. name
  150. special_from_date
  151. special_to_date
  152. new_to_date
  153. new_from_date
  154. tier_price
  155. manufacturer
  156. sku
  157. image {
  158. url
  159. label
  160. }
  161. canonical_url
  162. updated_at
  163. created_at
  164. categories {
  165. id
  166. position
  167. level
  168. url_key
  169. url_path
  170. product_count
  171. breadcrumbs {
  172. category_id
  173. category_name
  174. category_url_key
  175. }
  176. products {
  177. items {
  178. name
  179. special_from_date
  180. special_to_date
  181. price {
  182. minimalPrice {
  183. amount {
  184. value
  185. currency
  186. }
  187. }
  188. maximalPrice {
  189. amount {
  190. value
  191. currency
  192. }
  193. }
  194. regularPrice {
  195. amount {
  196. value
  197. currency
  198. }
  199. }
  200. }
  201. tier_price
  202. special_price
  203. tier_prices {
  204. customer_group_id
  205. qty
  206. percentage_value
  207. website_id
  208. }
  209. tier_prices {
  210. customer_group_id
  211. qty
  212. percentage_value
  213. website_id
  214. }
  215. tier_prices {
  216. customer_group_id
  217. qty
  218. percentage_value
  219. website_id
  220. }
  221. tier_prices {
  222. customer_group_id
  223. qty
  224. percentage_value
  225. website_id
  226. }
  227. tier_prices {
  228. customer_group_id
  229. qty
  230. percentage_value
  231. website_id
  232. }
  233. tier_prices {
  234. customer_group_id
  235. qty
  236. percentage_value
  237. website_id
  238. }
  239. tier_prices {
  240. customer_group_id
  241. qty
  242. percentage_value
  243. website_id
  244. }
  245. tier_prices {
  246. customer_group_id
  247. qty
  248. percentage_value
  249. website_id
  250. }
  251. tier_prices {
  252. customer_group_id
  253. qty
  254. percentage_value
  255. website_id
  256. }
  257. tier_prices {
  258. customer_group_id
  259. qty
  260. percentage_value
  261. website_id
  262. }
  263. tier_prices {
  264. customer_group_id
  265. qty
  266. percentage_value
  267. website_id
  268. }
  269. tier_prices {
  270. customer_group_id
  271. qty
  272. percentage_value
  273. website_id
  274. }
  275. tier_prices {
  276. customer_group_id
  277. qty
  278. percentage_value
  279. website_id
  280. }
  281. tier_prices {
  282. customer_group_id
  283. qty
  284. percentage_value
  285. website_id
  286. }
  287. tier_prices {
  288. customer_group_id
  289. qty
  290. percentage_value
  291. website_id
  292. }
  293. tier_prices {
  294. customer_group_id
  295. qty
  296. percentage_value
  297. website_id
  298. }
  299. tier_prices {
  300. customer_group_id
  301. qty
  302. percentage_value
  303. website_id
  304. }
  305. tier_prices {
  306. customer_group_id
  307. qty
  308. percentage_value
  309. website_id
  310. }
  311. tier_prices {
  312. customer_group_id
  313. qty
  314. percentage_value
  315. website_id
  316. }
  317. tier_prices {
  318. customer_group_id
  319. qty
  320. percentage_value
  321. website_id
  322. }
  323. new_to_date
  324. new_from_date
  325. tier_price
  326. manufacturer
  327. sku
  328. image {
  329. url
  330. label
  331. }
  332. thumbnail {
  333. url
  334. label
  335. }
  336. canonical_url
  337. updated_at
  338. created_at
  339. categories {
  340. id
  341. position
  342. position
  343. position
  344. position
  345. position
  346. position
  347. position
  348. position
  349. position
  350. position
  351. position
  352. position
  353. position
  354. position
  355. position
  356. position
  357. position
  358. position
  359. position
  360. level
  361. url_key
  362. url_path
  363. product_count
  364. default_sort_by
  365. breadcrumbs {
  366. category_id
  367. category_name
  368. category_url_key
  369. }
  370. }
  371. }
  372. }
  373. }
  374. }
  375. }
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. QUERY;
  391. self::expectExceptionMessageRegExp('/Max query complexity should be 300 but got 302/');
  392. $this->graphQlQuery($query);
  393. }
  394. /**
  395. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  396. */
  397. public function testQueryDepthIsLimited()
  398. {
  399. $query
  400. = <<<QUERY
  401. {
  402. category(id: 2) {
  403. products {
  404. items {
  405. name
  406. categories {
  407. products {
  408. items {
  409. media_gallery_entries {
  410. file
  411. }
  412. categories {
  413. products {
  414. items {
  415. categories {
  416. products {
  417. items {
  418. categories {
  419. products {
  420. items {
  421. categories {
  422. products {
  423. items {
  424. categories {
  425. products {
  426. items {
  427. categories {
  428. products {
  429. items {
  430. name
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. }
  438. }
  439. }
  440. }
  441. }
  442. }
  443. }
  444. }
  445. }
  446. }
  447. }
  448. }
  449. }
  450. }
  451. }
  452. }
  453. }
  454. }
  455. }
  456. QUERY;
  457. self::expectExceptionMessageRegExp('/Max query depth should be 20 but got 23/');
  458. $this->graphQlQuery($query);
  459. }
  460. }