ResultTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Controller;
  7. /**
  8. * @magentoDbIsolation enabled
  9. * @magentoAppIsolation enabled
  10. */
  11. class ResultTest extends \Magento\TestFramework\TestCase\AbstractController
  12. {
  13. /**
  14. * @magentoDataFixture Magento/CatalogSearch/_files/query.php
  15. */
  16. public function testIndexActionTranslation()
  17. {
  18. $this->markTestSkipped('MAGETWO-44910');
  19. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  20. $objectManager->get(\Magento\Framework\Locale\ResolverInterface::class)->setLocale('de_DE');
  21. $this->getRequest()->setParam('q', 'query_text');
  22. $this->dispatch('catalogsearch/result');
  23. $responseBody = $this->getResponse()->getBody();
  24. $this->assertNotContains('for="search">Search', $responseBody);
  25. $this->assertStringMatchesFormat('%aSuche%S%a', $responseBody);
  26. $this->assertNotContains('Search entire store here...', $responseBody);
  27. $this->assertContains('Den gesamten Shop durchsuchen...', $responseBody);
  28. }
  29. public function testIndexActionXSSQueryVerification()
  30. {
  31. $this->getRequest()->setParam('q', '<script>alert(1)</script>');
  32. $this->dispatch('catalogsearch/result');
  33. $responseBody = $this->getResponse()->getBody();
  34. $data = '<script>alert(1)</script>';
  35. $this->assertNotContains($data, $responseBody);
  36. $this->assertContains(htmlspecialchars($data, ENT_COMPAT, 'UTF-8', false), $responseBody);
  37. }
  38. /**
  39. * @magentoDataFixture Magento/CatalogSearch/_files/query_redirect.php
  40. */
  41. public function testRedirect()
  42. {
  43. $this->dispatch('/catalogsearch/result/?q=query_text');
  44. $responseBody = $this->getResponse();
  45. $this->assertTrue($responseBody->isRedirect());
  46. }
  47. /**
  48. * @magentoDataFixture Magento/CatalogSearch/_files/query_redirect.php
  49. */
  50. public function testNoRedirectIfCurrentUrlAndRedirectTermAreSame()
  51. {
  52. $this->dispatch('/catalogsearch/result/?q=query_text&cat=41');
  53. $responseBody = $this->getResponse();
  54. $this->assertFalse($responseBody->isRedirect());
  55. }
  56. /**
  57. * @magentoDataFixture Magento/CatalogSearch/_files/query.php
  58. */
  59. public function testPopularity()
  60. {
  61. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  62. /** @var $query \Magento\Search\Model\Query */
  63. $query = $objectManager->create(\Magento\Search\Model\Query::class);
  64. $query->loadByQueryText('query_text');
  65. $this->assertEquals(1, $query->getPopularity());
  66. $this->dispatch('catalogsearch/searchTermsLog/save?q=query_text');
  67. $responseBody = $this->getResponse()->getBody();
  68. $data = '"success":true';
  69. $this->assertContains($data, $responseBody);
  70. $query->loadByQueryText('query_text');
  71. $this->assertEquals(2, $query->getPopularity());
  72. }
  73. /**
  74. * @magentoDataFixture Magento/CatalogSearch/_files/popular_query.php
  75. * @magentoDataFixture Magento/CatalogSearch/_files/query.php
  76. */
  77. public function testPopularSearch()
  78. {
  79. $this->cacheAndPopularitySetup();
  80. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  81. /** @var $query \Magento\Search\Model\Query */
  82. $query = $objectManager->create(\Magento\Search\Model\Query::class);
  83. $query->loadByQueryText('popular_query_text');
  84. $this->assertEquals(100, $query->getPopularity());
  85. $this->dispatch('/catalogsearch/result/?q=popular_query_text');
  86. $responseBody = $this->getResponse()->getBody();
  87. $this->assertContains('Search results for: &#039;popular_query_text&#039;', $responseBody);
  88. $this->assertContains('/catalogsearch/searchTermsLog/save/', $responseBody);
  89. $query->loadByQueryText('popular_query_text');
  90. $this->assertEquals(100, $query->getPopularity());
  91. }
  92. /**
  93. * @magentoDataFixture Magento/CatalogSearch/_files/popular_query.php
  94. * @magentoDataFixture Magento/CatalogSearch/_files/query.php
  95. */
  96. public function testPopularSearchWithAdditionalRequestParameters()
  97. {
  98. $this->cacheAndPopularitySetup();
  99. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  100. /** @var $query \Magento\Search\Model\Query */
  101. $query = $objectManager->create(\Magento\Search\Model\Query::class);
  102. $query->loadByQueryText('popular_query_text');
  103. $this->assertEquals(100, $query->getPopularity());
  104. $this->dispatch('/catalogsearch/result/?q=popular_query_text&additional_parameters=some');
  105. $responseBody = $this->getResponse()->getBody();
  106. $this->assertContains('Search results for: &#039;popular_query_text&#039;', $responseBody);
  107. $this->assertNotContains('/catalogsearch/searchTermsLog/save/', $responseBody);
  108. $query->loadByQueryText('popular_query_text');
  109. $this->assertEquals(101, $query->getPopularity());
  110. }
  111. /**
  112. * @magentoDataFixture Magento/CatalogSearch/_files/popular_query.php
  113. * @magentoDataFixture Magento/CatalogSearch/_files/query.php
  114. */
  115. public function testNotPopularSearch()
  116. {
  117. $this->cacheAndPopularitySetup();
  118. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  119. /** @var $query \Magento\Search\Model\Query */
  120. $query = $objectManager->create(\Magento\Search\Model\Query::class);
  121. $query->loadByQueryText('query_text');
  122. $this->assertEquals(1, $query->getPopularity());
  123. $this->dispatch('/catalogsearch/result/?q=query_text');
  124. $responseBody = $this->getResponse()->getBody();
  125. $this->assertContains('Search results for: &#039;query_text&#039;', $responseBody);
  126. $this->assertNotContains('/catalogsearch/searchTermsLog/save/', $responseBody);
  127. $query->loadByQueryText('query_text');
  128. $this->assertEquals(2, $query->getPopularity());
  129. }
  130. private function cacheAndPopularitySetup()
  131. {
  132. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  133. /** @var $scopeConfig \Magento\Framework\App\MutableScopeConfig */
  134. $scopeConfig = $objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class);
  135. $scopeConfig->setValue(
  136. \Magento\Search\Model\PopularSearchTerms::XML_PATH_MAX_COUNT_CACHEABLE_SEARCH_TERMS,
  137. 1,
  138. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  139. );
  140. /** @var $cacheState \Magento\Framework\App\Cache\StateInterface */
  141. $cacheState = $objectManager->get(\Magento\Framework\App\Cache\StateInterface::class);
  142. $cacheState->setEnabled(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER, true);
  143. /** @var $fpc \Magento\PageCache\Model\Cache\Type */
  144. $fpc = $objectManager->get(\Magento\PageCache\Model\Cache\Type::class);
  145. $fpc->clean();
  146. }
  147. }