3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \SendFriend \Block ;
7
9
10
+ use Magento \Customer \Api \AccountManagementInterface ;
11
+ use Magento \Customer \Model \Session ;
12
+ use Magento \Framework \ObjectManagerInterface ;
13
+ use Magento \Framework \View \LayoutInterface ;
8
14
use Magento \TestFramework \Helper \Bootstrap ;
15
+ use Magento \TestFramework \Helper \Xpath ;
16
+ use PHPUnit \Framework \TestCase ;
9
17
10
- class SendTest extends \PHPUnit \Framework \TestCase
18
+ /**
19
+ * Class checks send friend email block
20
+ *
21
+ * @see \Magento\SendFriend\Block\Send
22
+ *
23
+ * @magentoAppArea frontend
24
+ */
25
+ class SendTest extends TestCase
11
26
{
27
+ /** @var array */
28
+ private $ elementsXpath = [
29
+ 'sender name field ' => "//input[@name='sender[name]'] " ,
30
+ 'sender email field ' => "//input[@name='sender[email]'] " ,
31
+ 'sender message field ' => "//textarea[@name='sender[message]'] " ,
32
+ 'recipient name field ' => "//input[contains(@name, 'recipients[name]')] " ,
33
+ 'recipient email field ' => "//input[contains(@name, 'recipients[email]')] " ,
34
+ 'submit button ' => "//button[@type='submit']/span[contains(text(), 'Send Email')] " ,
35
+ 'notice massage ' => "//div[@id='max-recipient-message'] "
36
+ . "/span[contains(text(), 'Maximum 1 email addresses allowed.')] "
37
+ ];
38
+
39
+ /** @var ObjectManagerInterface */
40
+ private $ objectManager ;
41
+
42
+ /** @var LayoutInterface */
43
+ private $ layout ;
44
+
45
+ /** @var Send */
46
+ private $ block ;
47
+
48
+ /** @var Session */
49
+ private $ session ;
50
+
51
+ /** @var AccountManagementInterface */
52
+ private $ accountManagement ;
53
+
12
54
/**
13
- * @var \Magento\SendFriend\Block\Send
55
+ * @inheritdoc
14
56
*/
15
- protected $ _block ;
16
-
17
57
protected function setUp ()
18
58
{
19
- $ this ->_block = Bootstrap::getObjectManager ()->create (\Magento \SendFriend \Block \Send::class);
59
+ $ this ->objectManager = Bootstrap::getObjectManager ();
60
+ $ this ->layout = $ this ->objectManager ->get (LayoutInterface::class);
61
+ $ this ->block = $ this ->layout ->createBlock (Send::class);
62
+ $ this ->session = $ this ->objectManager ->get (Session::class);
63
+ $ this ->accountManagement = $ this ->objectManager ->get (AccountManagementInterface::class);
64
+ }
65
+
66
+ /**
67
+ * @inheritdoc
68
+ */
69
+ protected function tearDown ()
70
+ {
71
+ parent ::tearDown ();
72
+
73
+ $ this ->session ->logout ();
20
74
}
21
75
22
76
/**
77
+ * @dataProvider formDataProvider
78
+ *
23
79
* @param string $field
24
80
* @param string $value
25
- * @dataProvider formDataProvider
26
- * @covers \Magento\SendFriend\Block\Send::getUserName
27
- * @covers \Magento\SendFriend\Block\Send::getEmail
81
+ * @return void
28
82
*/
29
- public function testGetCustomerFieldFromFormData ($ field , $ value )
83
+ public function testGetCustomerFieldFromFormData (string $ field , string $ value ): void
30
84
{
31
85
$ formData = ['sender ' => [$ field => $ value ]];
32
- $ this ->_block ->setFormData ($ formData );
86
+ $ this ->block ->setFormData ($ formData );
33
87
$ this ->assertEquals (trim ($ value ), $ this ->_callBlockMethod ($ field ));
34
88
}
35
89
36
90
/**
37
91
* @return array
38
92
*/
39
- public function formDataProvider ()
93
+ public function formDataProvider (): array
40
94
{
41
95
return [
42
96
['name ' , 'Customer Form Name ' ],
@@ -45,49 +99,65 @@ public function formDataProvider()
45
99
}
46
100
47
101
/**
102
+ * @magentoDataFixture Magento/Customer/_files/customer.php
103
+ *
104
+ * @dataProvider customerSessionDataProvider
105
+ *
106
+ * @magentoAppIsolation enabled
107
+ *
48
108
* @param string $field
49
109
* @param string $value
50
- * @dataProvider customerSessionDataProvider
51
- * @covers \Magento\SendFriend\Block\Send::getUserName
52
- * @covers \Magento\SendFriend\Block\Send::getEmail
53
- * @magentoDataFixture Magento/Customer/_files/customer.php
110
+ * @return void
54
111
*/
55
- public function testGetCustomerFieldFromSession ($ field , $ value )
112
+ public function testGetCustomerFieldFromSession (string $ field , string $ value ): void
56
113
{
57
- $ logger = $ this ->createMock (\Psr \Log \LoggerInterface::class);
58
- /** @var $session \Magento\Customer\Model\Session */
59
- $ session = Bootstrap::getObjectManager ()->create (\Magento \Customer \Model \Session::class, [$ logger ]);
60
- /** @var \Magento\Customer\Api\AccountManagementInterface $service */
61
- $ service = Bootstrap::getObjectManager ()->create (\Magento \Customer \Api \AccountManagementInterface::class);
62
- $ customer = $ service ->authenticate ('customer@example.com ' , 'password ' );
63
- $ session ->setCustomerDataAsLoggedIn ($ customer );
114
+ $ customer = $ this ->accountManagement ->authenticate ('customer@example.com ' , 'password ' );
115
+ $ this ->session ->setCustomerDataAsLoggedIn ($ customer );
64
116
$ this ->assertEquals ($ value , $ this ->_callBlockMethod ($ field ));
65
117
}
66
118
67
119
/**
68
120
* @return array
69
121
*/
70
- public function customerSessionDataProvider ()
122
+ public function customerSessionDataProvider (): array
71
123
{
72
124
return [
73
125
['name ' , 'John Smith ' ],
74
126
['email ' , 'customer@example.com ' ]
75
127
];
76
128
}
77
129
130
+ /**
131
+ * @magentoConfigFixture current_store sendfriend/email/max_recipients 1
132
+ *
133
+ * @return void
134
+ */
135
+ public function testBlockAppearance (): void
136
+ {
137
+ $ this ->block ->setTemplate ('Magento_SendFriend::send.phtml ' );
138
+ $ html = preg_replace ('#<script(.*?)>#i ' , '' , $ this ->block ->toHtml ());
139
+ foreach ($ this ->elementsXpath as $ key => $ xpath ) {
140
+ $ this ->assertEquals (
141
+ 1 ,
142
+ Xpath::getElementsCountForXpath ($ xpath , $ html ),
143
+ sprintf ('The %s field is not found on the page ' , $ key )
144
+ );
145
+ }
146
+ }
147
+
78
148
/**
79
149
* Call block method based on form field
80
150
*
81
151
* @param string $field
82
152
* @return null|string
83
153
*/
84
- protected function _callBlockMethod ($ field )
154
+ protected function _callBlockMethod (string $ field ): ? string
85
155
{
86
156
switch ($ field ) {
87
157
case 'name ' :
88
- return $ this ->_block ->getUserName ();
158
+ return $ this ->block ->getUserName ();
89
159
case 'email ' :
90
- return $ this ->_block ->getEmail ();
160
+ return $ this ->block ->getEmail ();
91
161
default :
92
162
return null ;
93
163
}
0 commit comments