5
5
* Copyright © Magento, Inc. All rights reserved.
6
6
* See COPYING.txt for license details.
7
7
*/
8
+
9
+ declare (strict_types=1 );
10
+
8
11
namespace Magento \Sales \Block \Adminhtml \Order \Create \Form ;
9
12
13
+ use Magento \Backend \Model \Session \Quote as SessionQuote ;
14
+ use Magento \Customer \Api \Data \AttributeMetadataInterfaceFactory ;
15
+ use Magento \Customer \Model \Metadata \Form ;
16
+ use Magento \Customer \Model \Metadata \FormFactory ;
17
+ use Magento \Framework \View \LayoutInterface ;
18
+ use Magento \Quote \Model \Quote ;
19
+ use Magento \TestFramework \Helper \Bootstrap ;
20
+
10
21
/**
11
22
* @magentoAppArea adminhtml
12
23
*/
13
24
class AccountTest extends \PHPUnit \Framework \TestCase
14
25
{
15
- /** @var \Magento\Sales\Block\Adminhtml\Order\Create\Form\ Account */
16
- protected $ _accountBlock ;
26
+ /** @var Account */
27
+ private $ accountBlock ;
17
28
18
29
/**
19
- * @var \Magento\TestFramework\Helper\ Bootstrap
30
+ * @var Bootstrap
20
31
*/
21
- protected $ _objectManager ;
32
+ private $ objectManager ;
22
33
23
34
/**
24
35
* @magentoDataFixture Magento/Sales/_files/quote.php
25
36
*/
26
37
protected function setUp ()
27
38
{
28
- $ this ->_objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
29
- $ quote = $ this ->_objectManager ->create (\ Magento \ Quote \ Model \ Quote::class)->load (1 );
39
+ $ this ->objectManager = Bootstrap::getObjectManager ();
40
+ $ quote = $ this ->objectManager ->create (Quote::class)->load (1 );
30
41
$ sessionQuoteMock = $ this ->getMockBuilder (
31
- \ Magento \ Backend \ Model \ Session \Quote ::class
42
+ SessionQuote ::class
32
43
)->disableOriginalConstructor ()->setMethods (
33
44
['getCustomerId ' , 'getStore ' , 'getStoreId ' , 'getQuote ' ]
34
45
)->getMock ();
35
46
$ sessionQuoteMock ->expects ($ this ->any ())->method ('getCustomerId ' )->will ($ this ->returnValue (1 ));
36
47
$ sessionQuoteMock ->expects ($ this ->any ())->method ('getQuote ' )->will ($ this ->returnValue ($ quote ));
37
- /** @var \Magento\Framework\View\ LayoutInterface $layout */
38
- $ layout = $ this ->_objectManager ->get (\ Magento \ Framework \ View \ LayoutInterface::class);
39
- $ this ->_accountBlock = $ layout ->createBlock (
40
- \ Magento \ Sales \ Block \ Adminhtml \ Order \ Create \ Form \ Account::class,
48
+ /** @var LayoutInterface $layout */
49
+ $ layout = $ this ->objectManager ->get (LayoutInterface::class);
50
+ $ this ->accountBlock = $ layout ->createBlock (
51
+ Account::class,
41
52
'address_block ' . rand (),
42
53
['sessionQuote ' => $ sessionQuoteMock ]
43
54
);
@@ -50,7 +61,7 @@ protected function setUp()
50
61
public function testGetForm ()
51
62
{
52
63
$ expectedFields = ['group_id ' , 'email ' ];
53
- $ form = $ this ->_accountBlock ->getForm ();
64
+ $ form = $ this ->accountBlock ->getForm ();
54
65
$ this ->assertEquals (1 , $ form ->getElements ()->count (), "Form has invalid number of fieldsets " );
55
66
$ fieldset = $ form ->getElements ()[0 ];
56
67
@@ -63,4 +74,56 @@ public function testGetForm()
63
74
);
64
75
}
65
76
}
77
+
78
+ /**
79
+ * Tests a case when user defined custom attribute has default value.
80
+ *
81
+ * @magentoDataFixture Magento/Customer/_files/customer.php
82
+ */
83
+ public function testGetFormWithUserDefinedAttribute ()
84
+ {
85
+ $ formFactory = $ this ->getFormFactoryMock ();
86
+ $ this ->objectManager ->addSharedInstance ($ formFactory , FormFactory::class);
87
+
88
+ /** @var LayoutInterface $layout */
89
+ $ layout = $ this ->objectManager ->get (LayoutInterface::class);
90
+ $ accountBlock = $ layout ->createBlock (Account::class, 'address_block ' . rand ());
91
+
92
+ $ form = $ accountBlock ->getForm ();
93
+ $ form ->setUseContainer (true );
94
+
95
+ $ this ->assertContains (
96
+ '<option value="1" selected="selected">Yes</option> ' ,
97
+ $ form ->toHtml (),
98
+ 'Default value for user defined custom attribute should be selected '
99
+ );
100
+ }
101
+
102
+ /**
103
+ * @return \PHPUnit_Framework_MockObject_MockObject
104
+ */
105
+ private function getFormFactoryMock (): \PHPUnit_Framework_MockObject_MockObject
106
+ {
107
+ /** @var AttributeMetadataInterfaceFactory $attributeMetadataFactory */
108
+ $ attributeMetadataFactory = $ this ->objectManager ->create (AttributeMetadataInterfaceFactory::class);
109
+ $ booleanAttribute = $ attributeMetadataFactory ->create ()
110
+ ->setAttributeCode ('boolean ' )
111
+ ->setBackendType ('boolean ' )
112
+ ->setFrontendInput ('boolean ' )
113
+ ->setDefaultValue ('1 ' )
114
+ ->setFrontendLabel ('Yes/No ' );
115
+
116
+ $ form = $ this ->getMockBuilder (Form::class)
117
+ ->disableOriginalConstructor ()
118
+ ->getMock ();
119
+ $ form ->method ('getUserAttributes ' )->willReturn ([$ booleanAttribute ]);
120
+ $ form ->method ('getSystemAttributes ' )->willReturn ([]);
121
+
122
+ $ formFactory = $ this ->getMockBuilder (FormFactory::class)
123
+ ->disableOriginalConstructor ()
124
+ ->getMock ();
125
+ $ formFactory ->method ('create ' )->willReturn ($ form );
126
+
127
+ return $ formFactory ;
128
+ }
66
129
}
0 commit comments