GaTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\GoogleAnalytics\Block;
  8. use Magento\TestFramework\TestCase\AbstractController;
  9. class GaTest extends AbstractController
  10. {
  11. /**
  12. * Layout instance
  13. *
  14. * @var \Magento\Framework\View\LayoutInterface
  15. */
  16. private $layout;
  17. /**
  18. * @inheritdoc
  19. */
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $this->dispatch('/');
  24. $this->layout = $this->_objectManager->get(\Magento\Framework\View\LayoutInterface::class);
  25. }
  26. /**
  27. * Check for correct position of GA block
  28. */
  29. public function testBlockPresentInHead()
  30. {
  31. $this->assertNotNull(
  32. $this->getGaBlockFromNode('head.additional')
  33. );
  34. }
  35. /**
  36. * Test that block has been successfully moved
  37. * from body to head tag.
  38. */
  39. public function testBlockIsAbsentInBody()
  40. {
  41. $this->assertFalse(
  42. $this->getGaBlockFromNode('after.body.start')
  43. );
  44. }
  45. /**
  46. * Test null output when GA is disabled
  47. *
  48. * @magentoAppArea frontend
  49. * @magentoConfigFixture current_store google/analytics/active 0
  50. * @magentoConfigFixture current_store google/analytics/account XXXXXXX
  51. */
  52. public function testBlockOutputIsEmptyWhenGaIsDisabled()
  53. {
  54. $this->assertEquals(
  55. "",
  56. $this->getGaBlockFromNode('head.additional')->toHtml()
  57. );
  58. }
  59. /**
  60. * Check, that block successfully gets rendered when configuration is
  61. * active.
  62. *
  63. * @magentoAppArea frontend
  64. * @magentoConfigFixture current_store google/analytics/active 1
  65. * @magentoConfigFixture current_store google/analytics/account XXXXXXX
  66. */
  67. public function testBlockOutputExistsWhenGaIsEnabled()
  68. {
  69. $this->assertNotEquals(
  70. "",
  71. $this->getGaBlockFromNode('head.additional')->toHtml()
  72. );
  73. }
  74. /**
  75. * Get GA block
  76. *
  77. * @param string $nodeName
  78. * @return \Magento\Framework\View\Element\AbstractBlock|false
  79. */
  80. private function getGaBlockFromNode($nodeName = 'head.additional')
  81. {
  82. $childBlocks = $this->layout->getChildBlocks($nodeName);
  83. foreach ($childBlocks as $block) {
  84. if (strpos($block->getNameInLayout(), 'google_analytics') !== false) {
  85. return $block;
  86. }
  87. }
  88. return false;
  89. }
  90. }