5
5
*/
6
6
namespace Magento \Theme \Test \Unit \Block \Html ;
7
7
8
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
9
+ use Magento \Framework \Phrase ;
8
10
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
11
+ use Magento \Framework \View \Element \Template \Context ;
12
+ use Magento \Framework \View \Page \Config ;
13
+ use Magento \Framework \View \Page \Title as PageTitle ;
14
+ use Magento \Store \Model \ScopeInterface ;
15
+ use Magento \Theme \Block \Html \Title ;
16
+ use PHPUnit \Framework \MockObject \MockObject ;
17
+ use PHPUnit \Framework \TestCase ;
9
18
10
- class TitleTest extends \PHPUnit \Framework \TestCase
19
+ /**
20
+ * Test class for \Magento\Theme\Block\Html\Title
21
+ */
22
+ class TitleTest extends TestCase
11
23
{
24
+ /**
25
+ * Config path to 'Translate Title' header settings
26
+ */
27
+ private const XML_PATH_HEADER_TRANSLATE_TITLE = 'design/header/translate_title ' ;
28
+
12
29
/**
13
30
* @var ObjectManagerHelper
14
31
*/
15
- protected $ objectManagerHelper ;
32
+ private $ objectManagerHelper ;
33
+
34
+ /**
35
+ * @var Config|MockObject
36
+ */
37
+ private $ pageConfigMock ;
16
38
17
39
/**
18
- * @var \Magento\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
40
+ * @var PageTitle|MockObject
19
41
*/
20
- protected $ pageConfigMock ;
42
+ private $ pageTitleMock ;
21
43
22
44
/**
23
- * @var \Magento\Framework\View\Page\Title|\PHPUnit_Framework_MockObject_MockObject
45
+ * @var ScopeConfigInterface|MockObject
24
46
*/
25
- protected $ pageTitleMock ;
47
+ private $ scopeConfigMock ;
26
48
27
49
/**
28
- * @var \Magento\Theme\Block\Html\ Title
50
+ * @var Title
29
51
*/
30
- protected $ htmlTitle ;
52
+ private $ htmlTitle ;
31
53
32
54
/**
33
55
* @return void
34
56
*/
35
57
protected function setUp ()
36
58
{
37
59
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
38
- $ this ->pageConfigMock = $ this ->createMock (\ Magento \ Framework \ View \ Page \ Config::class);
39
- $ this ->pageTitleMock = $ this ->createMock (\ Magento \ Framework \ View \ Page \Title ::class);
60
+ $ this ->pageConfigMock = $ this ->createMock (Config::class);
61
+ $ this ->pageTitleMock = $ this ->createMock (PageTitle ::class);
40
62
41
63
$ context = $ this ->objectManagerHelper ->getObject (
42
- \ Magento \ Framework \ View \ Element \ Template \ Context::class,
64
+ Context::class,
43
65
['pageConfig ' => $ this ->pageConfigMock ]
44
66
);
45
67
68
+ $ this ->scopeConfigMock = $ this ->getMockForAbstractClass (ScopeConfigInterface::class);
69
+
46
70
$ this ->htmlTitle = $ this ->objectManagerHelper ->getObject (
47
- \Magento \Theme \Block \Html \Title::class,
48
- ['context ' => $ context ]
71
+ Title::class,
72
+ [
73
+ 'context ' => $ context ,
74
+ 'scopeConfig ' => $ this ->scopeConfigMock
75
+ ]
49
76
);
50
77
}
51
78
@@ -64,10 +91,16 @@ public function testGetPageTitleWithSetPageTitle()
64
91
}
65
92
66
93
/**
94
+ * @param bool $shouldTranslateTitle
95
+ *
67
96
* @return void
97
+ * @dataProvider dataProviderShouldTranslateTitle
68
98
*/
69
- public function testGetPageTitle ()
99
+ public function testGetPageTitle ($ shouldTranslateTitle )
70
100
{
101
+ $ this ->scopeConfigMock ->method ('isSetFlag ' )
102
+ ->with (static ::XML_PATH_HEADER_TRANSLATE_TITLE , ScopeInterface::SCOPE_STORE )
103
+ ->willReturn ($ shouldTranslateTitle );
71
104
$ title = 'some title ' ;
72
105
73
106
$ this ->pageTitleMock ->expects ($ this ->once ())
@@ -77,28 +110,58 @@ public function testGetPageTitle()
77
110
->method ('getTitle ' )
78
111
->willReturn ($ this ->pageTitleMock );
79
112
80
- $ this ->assertEquals ($ title , $ this ->htmlTitle ->getPageTitle ());
113
+ $ result = $ this ->htmlTitle ->getPageTitle ();
114
+
115
+ if ($ shouldTranslateTitle ) {
116
+ $ this ->assertInstanceOf (Phrase::class, $ result );
117
+ } else {
118
+ $ this ->assertInternalType ('string ' , $ result );
119
+ }
120
+
121
+ $ this ->assertEquals ($ title , $ result );
81
122
}
82
123
83
124
/**
125
+ * @param bool $shouldTranslateTitle
126
+ *
84
127
* @return void
128
+ * @dataProvider dataProviderShouldTranslateTitle
85
129
*/
86
- public function testGetPageHeadingWithSetPageTitle ()
130
+ public function testGetPageHeadingWithSetPageTitle ($ shouldTranslateTitle )
87
131
{
132
+ $ this ->scopeConfigMock ->method ('isSetFlag ' )
133
+ ->with (static ::XML_PATH_HEADER_TRANSLATE_TITLE , ScopeInterface::SCOPE_STORE )
134
+ ->willReturn ($ shouldTranslateTitle );
135
+
88
136
$ title = 'some title ' ;
89
137
90
138
$ this ->htmlTitle ->setPageTitle ($ title );
91
139
$ this ->pageConfigMock ->expects ($ this ->never ())
92
140
->method ('getTitle ' );
93
141
94
- $ this ->assertEquals ($ title , $ this ->htmlTitle ->getPageHeading ());
142
+ $ result = $ this ->htmlTitle ->getPageHeading ();
143
+
144
+ if ($ shouldTranslateTitle ) {
145
+ $ this ->assertInstanceOf (Phrase::class, $ result );
146
+ } else {
147
+ $ this ->assertInternalType ('string ' , $ result );
148
+ }
149
+
150
+ $ this ->assertEquals ($ title , $ result );
95
151
}
96
152
97
153
/**
154
+ * @param bool $shouldTranslateTitle
155
+ *
98
156
* @return void
157
+ * @dataProvider dataProviderShouldTranslateTitle
99
158
*/
100
- public function testGetPageHeading ()
159
+ public function testGetPageHeading ($ shouldTranslateTitle )
101
160
{
161
+ $ this ->scopeConfigMock ->method ('isSetFlag ' )
162
+ ->with (static ::XML_PATH_HEADER_TRANSLATE_TITLE , ScopeInterface::SCOPE_STORE )
163
+ ->willReturn ($ shouldTranslateTitle );
164
+
102
165
$ title = 'some title ' ;
103
166
104
167
$ this ->pageTitleMock ->expects ($ this ->once ())
@@ -108,6 +171,29 @@ public function testGetPageHeading()
108
171
->method ('getTitle ' )
109
172
->willReturn ($ this ->pageTitleMock );
110
173
111
- $ this ->assertEquals ($ title , $ this ->htmlTitle ->getPageHeading ());
174
+ $ result = $ this ->htmlTitle ->getPageHeading ();
175
+
176
+ if ($ shouldTranslateTitle ) {
177
+ $ this ->assertInstanceOf (Phrase::class, $ result );
178
+ } else {
179
+ $ this ->assertInternalType ('string ' , $ result );
180
+ }
181
+
182
+ $ this ->assertEquals ($ title , $ result );
183
+ }
184
+
185
+ /**
186
+ * @return array
187
+ */
188
+ public function dataProviderShouldTranslateTitle (): array
189
+ {
190
+ return [
191
+ [
192
+ true
193
+ ],
194
+ [
195
+ false
196
+ ]
197
+ ];
112
198
}
113
199
}
0 commit comments