Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
HeadTest.php
Go to the documentation of this file.
1 <?php
8 
15 
19 class HeadTest extends \PHPUnit\Framework\TestCase
20 {
24  protected $headGenerator;
25 
29  protected $pageConfigMock;
30 
34  protected $urlMock;
35 
39  protected $title;
40 
41  protected function setUp()
42  {
43  $this->pageConfigMock = $this->getMockBuilder(\Magento\Framework\View\Page\Config::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46  $this->title = $this->getMockBuilder(\Magento\Framework\View\Page\Title::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  $this->urlMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
50  ->disableOriginalConstructor()
51  ->getMock();
52 
53  $objectManagerHelper = new ObjectManagerHelper($this);
54  $this->headGenerator = $objectManagerHelper->getObject(
55  \Magento\Framework\View\Page\Config\Generator\Head::class,
56  [
57  'pageConfig' => $this->pageConfigMock,
58  'url' => $this->urlMock,
59  ]
60  );
61  }
62 
66  public function testProcess()
67  {
68  $generatorContextMock = $this->createMock(Context::class);
69  $this->title->expects($this->any())->method('set')->with()->will($this->returnSelf());
70  $structureMock = $this->createMock(Structure::class);
71  $readerContextMock = $this->createMock(ReaderContext::class);
72  $readerContextMock->expects($this->any())->method('getPageConfigStructure')->willReturn($structureMock);
73 
74  $structureMock->expects($this->once())->method('processRemoveAssets');
75  $structureMock->expects($this->once())->method('processRemoveElementAttributes');
76  $this->urlMock->expects($this->once())
77  ->method('getUrl')
78  ->with('customcss/render/css')
79  ->willReturn('http://magento.dev/customcss/render/css');
80 
81  $assets = [
82  'remoteCss' => [
83  'src' => 'file-url-css',
84  'src_type' => 'url',
85  'content_type' => 'css',
86  'media' => 'all',
87  ],
88  'remoteCssOrderedLast' => [
89  'src' => 'file-url-css-last',
90  'src_type' => 'url',
91  'content_type' => 'css',
92  'media' => 'all',
93  'order' => 30,
94  ],
95  'remoteCssOrderedFirst' => [
96  'src' => 'file-url-css-first',
97  'src_type' => 'url',
98  'content_type' => 'css',
99  'media' => 'all',
100  'order' => 10,
101  ],
102  'remoteLink' => [
103  'src' => 'file-url-link',
104  'src_type' => 'url',
105  'media' => 'all',
106  ],
107  'controllerCss' => [
108  'src' => 'customcss/render/css',
109  'src_type' => 'controller',
110  'content_type' => 'css',
111  'media' => 'all',
112  ],
113  'name' => [
114  'src' => 'file-path',
115  'ie_condition' => 'lt IE 7',
116  'content_type' => 'css',
117  'media' => 'print',
118  ],
119  ];
120 
121  $this->pageConfigMock->expects($this->at(0))
122  ->method('addRemotePageAsset')
123  ->with('file-url-css', 'css', ['attributes' => ['media' => 'all']]);
124  $this->pageConfigMock->expects($this->at(1))
125  ->method('addRemotePageAsset')
126  ->with('file-url-css-last', 'css', ['attributes' => ['media' => 'all' ] , 'order' => 30]);
127  $this->pageConfigMock->expects($this->at(2))
128  ->method('addRemotePageAsset')
129  ->with('file-url-css-first', 'css', ['attributes' => ['media' => 'all'] , 'order' => 10]);
130  $this->pageConfigMock->expects($this->at(3))
131  ->method('addRemotePageAsset')
132  ->with('file-url-link', Head::VIRTUAL_CONTENT_TYPE_LINK, ['attributes' => ['media' => 'all']]);
133  $this->pageConfigMock->expects($this->at(4))
134  ->method('addRemotePageAsset')
135  ->with('http://magento.dev/customcss/render/css', 'css', ['attributes' => ['media' => 'all']]);
136  $this->pageConfigMock->expects($this->once())
137  ->method('addPageAsset')
138  ->with('name', ['attributes' => ['media' => 'print'], 'ie_condition' => 'lt IE 7']);
139  $structureMock->expects($this->once())
140  ->method('getAssets')
141  ->will($this->returnValue($assets));
142 
143  $title = 'Page title';
144  $structureMock->expects($this->atLeastOnce())
145  ->method('getTitle')
146  ->will($this->returnValue($title));
147  $this->pageConfigMock->expects($this->any())->method('getTitle')->will($this->returnValue($this->title));
148 
149  $metadata = ['name1' => 'content1', 'name2' => 'content2'];
150  $structureMock->expects($this->once())
151  ->method('getMetadata')
152  ->will($this->returnValue($metadata));
153  $this->pageConfigMock->expects($this->exactly(2))
154  ->method('setMetadata')
155  ->withConsecutive(['name1', 'content1'], ['name2', 'content2']);
156 
157  $elementAttributes = [
158  PageConfig::ELEMENT_TYPE_BODY => [
159  'body_attr_1' => 'body_value_1',
160  'body_attr_2' => 'body_value_2',
161  ],
162  PageConfig::ELEMENT_TYPE_HTML => [
163  'html_attr_1' => 'html_attr_1',
164  ],
165  ];
166  $structureMock->expects($this->once())
167  ->method('getElementAttributes')
168  ->will($this->returnValue($elementAttributes));
169  $this->pageConfigMock->expects($this->exactly(3))
170  ->method('setElementAttribute')
171  ->withConsecutive(
172  [PageConfig::ELEMENT_TYPE_BODY, 'body_attr_1', 'body_value_1'],
173  [PageConfig::ELEMENT_TYPE_BODY, 'body_attr_2', 'body_value_2'],
174  [PageConfig::ELEMENT_TYPE_HTML, 'html_attr_1', 'html_attr_1']
175  );
176 
177  $result = $this->headGenerator->process($readerContextMock, $generatorContextMock);
178  $this->assertEquals($this->headGenerator, $result);
179  }
180 }