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