1
+ <?php
2
+ /**
3
+ * Copyright © 2015 Magento. All rights reserved.
4
+ * See COPYING.txt for license details.
5
+ */
6
+
7
+ // @codingStandardsIgnoreFile
8
+
9
+ namespace Magento \Store \Model ;
10
+
11
+ use Magento \Framework \Store \StoreManagerInterface ;
12
+ use Magento \Framework \Store \ScopeInterface ;
13
+ use Magento \Store \Model \Store ;
14
+ use Magento \TestFramework \Helper \ObjectManager ;
15
+
16
+ class SecureUrlTest extends \PHPUnit_Framework_TestCase
17
+ {
18
+ /** @var \Magento\Framework\App\Config\ScopeConfigInterface | \PHPUnit_Framework_MockObject_MockObject*/
19
+ private $ scopeConfigMock ;
20
+ /** @var \Magento\Framework\Url\SecurityInfoInterface | \PHPUnit_Framework_MockObject_MockObject*/
21
+ private $ urlSecurityInfoMock ;
22
+ /** @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject*/
23
+ private $ storeManagerMock ;
24
+ /** @var Store | \PHPUnit_Framework_MockObject_MockObject*/
25
+ private $ storeMock ;
26
+ /** @var \Magento\Store\Model\SecureUrl */
27
+ protected $ model ;
28
+
29
+ public function setUp ()
30
+ {
31
+ $ this ->scopeConfigMock = $ this ->getMockBuilder ('Magento\Framework\App\Config\ScopeConfigInterface ' )
32
+ ->disableOriginalConstructor ()
33
+ ->getMock ();
34
+ $ this ->urlSecurityInfoMock = $ this ->getMockBuilder ('Magento\Framework\Url\SecurityInfoInterface ' )
35
+ ->disableOriginalConstructor ()
36
+ ->getMock ();
37
+ $ this ->storeManagerMock = $ this ->getMockBuilder ('Magento\Framework\Store\StoreManagerInterface ' )
38
+ ->disableOriginalConstructor ()
39
+ ->getMock ();
40
+ $ this ->storeMock = $ this ->getMockBuilder ('Magento\Store\Model\Store ' )
41
+ ->disableOriginalConstructor ()
42
+ ->getMock ();
43
+ $ mockArgs = [
44
+ 'scopeConfig ' => $ this ->scopeConfigMock ,
45
+ 'urlSecurityInfo ' => $ this ->urlSecurityInfoMock ,
46
+ 'storeManager ' => $ this ->storeManagerMock ,
47
+ ];
48
+ $ this ->model = (new ObjectManager ($ this ))->getObject ('\Magento\Store\Model\SecureUrl ' , $ mockArgs );
49
+ }
50
+
51
+ public function testGetCurrentSecureUrlNoAlias ()
52
+ {
53
+ $ baseUrl = 'base-store.url/ ' ;
54
+ $ pathInfo = 'path/to/action ' ;
55
+
56
+ $ this ->storeMock ->expects ($ this ->once ())->method ('getBaseUrl ' )->with ('link ' , true )->willReturn ($ baseUrl );
57
+ $ this ->storeManagerMock ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ this ->storeMock );
58
+
59
+ $ request = $ this ->getMockBuilder ('Magento\Framework\App\Request\Http ' )
60
+ ->disableOriginalConstructor ()
61
+ ->getMock ();
62
+
63
+ $ request ->expects ($ this ->once ())->method ('getAlias ' )->willReturn (null );
64
+ $ request ->expects ($ this ->once ())->method ('getPathInfo ' )->willReturn ($ pathInfo );
65
+ $ this ->assertSame ($ baseUrl . $ pathInfo , $ this ->model ->getCurrentSecureUrl ($ request ));
66
+ }
67
+
68
+ public function testGetCurrentSecureUrlWithAlias ()
69
+ {
70
+ $ baseUrl = 'base-store.url/ ' ;
71
+ $ alias = 'action-alias ' ;
72
+
73
+ $ this ->storeMock ->expects ($ this ->once ())->method ('getBaseUrl ' )->with ('link ' , true )->willReturn ($ baseUrl );
74
+ $ this ->storeManagerMock ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ this ->storeMock );
75
+
76
+ $ request = $ this ->getMockBuilder ('Magento\Framework\App\Request\Http ' )
77
+ ->disableOriginalConstructor ()
78
+ ->getMock ();
79
+
80
+ $ request ->expects ($ this ->once ())->method ('getAlias ' )->willReturn ($ alias );
81
+ $ request ->expects ($ this ->never ())->method ('getPathInfo ' );
82
+ $ this ->assertSame ($ baseUrl . $ alias , $ this ->model ->getCurrentSecureUrl ($ request ));
83
+ }
84
+
85
+ /**
86
+ * @dataProvider urlSchemeProvider
87
+ * @param string $base Base Url
88
+ * @param bool $secure Expected return value
89
+ */
90
+ public function testShouldBeSecureUnsecureBaseUrl ($ base , $ secure )
91
+ {
92
+ $ this ->scopeConfigMock ->expects ($ this ->once ())
93
+ ->method ('getValue ' )
94
+ ->with (Store::XML_PATH_UNSECURE_BASE_URL , ScopeInterface::SCOPE_STORE )
95
+ ->willReturn ($ base );
96
+ $ this ->assertSame ($ secure , $ this ->model ->shouldBeSecure ('path/to/action ' ));
97
+ }
98
+
99
+ /**
100
+ * @dataProvider urlSchemeProvider
101
+ * @param string $base Base Url
102
+ * @param bool $secure Expected return value
103
+ */
104
+ public function testShouldBeSecureSecureBaseUrl ($ base , $ secure )
105
+ {
106
+ $ path = 'path/to/action ' ;
107
+
108
+ $ this ->scopeConfigMock ->expects ($ this ->once ())->method ('isSetFlag ' )
109
+ ->with (Store::XML_PATH_SECURE_IN_FRONTEND , ScopeInterface::SCOPE_STORE )
110
+ ->willReturn ($ secure );
111
+
112
+ $ getValueReturnMap = [
113
+ [Store::XML_PATH_SECURE_BASE_URL , ScopeInterface::SCOPE_STORE , null , $ base ],
114
+ [Store::XML_PATH_UNSECURE_BASE_URL , ScopeInterface::SCOPE_STORE , null , 'http://unsecure.url ' ],
115
+ ];
116
+
117
+ $ this ->scopeConfigMock ->expects ($ this ->any ())
118
+ ->method ('getValue ' )
119
+ ->will ($ this ->returnValueMap ($ getValueReturnMap ));
120
+
121
+ if ($ secure ) {
122
+ $ this ->urlSecurityInfoMock ->expects ($ this ->once ())->method ('isSecure ' )->with ($ path )->willReturn ($ secure );
123
+ }
124
+
125
+ $ this ->assertSame ($ secure , $ this ->model ->shouldBeSecure ($ path ));
126
+ }
127
+
128
+ /**
129
+ * @return array
130
+ */
131
+ public function urlSchemeProvider ()
132
+ {
133
+ return [
134
+ ['https://base.url ' , true ],
135
+ ['http://base.url ' , false ]
136
+ ];
137
+ }
138
+ }
0 commit comments