FormTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test for \Magento\Eav\Model\Form
  8. */
  9. namespace Magento\Eav\Test\Unit\Model;
  10. class FormTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Eav\Model\Form
  14. */
  15. protected $_model = null;
  16. /**
  17. * @var array
  18. */
  19. protected $_attributes = null;
  20. /**
  21. * @var array
  22. */
  23. protected $_systemAttribute = null;
  24. /**
  25. * @var array
  26. */
  27. protected $_userAttribute = null;
  28. /**
  29. * @var \Magento\Framework\DataObject
  30. */
  31. protected $_entity = null;
  32. /**
  33. * Initialize form
  34. */
  35. protected function setUp()
  36. {
  37. $this->_model = $this->getMockBuilder(
  38. \Magento\Eav\Model\Form::class
  39. )->setMethods(
  40. ['_getFilteredFormAttributeCollection', '_getValidator', 'getEntity']
  41. )->disableOriginalConstructor()->getMock();
  42. $this->_userAttribute = new \Magento\Framework\DataObject(
  43. ['is_user_defined' => true, 'attribute_code' => 'attribute_visible_user', 'is_visible' => true]
  44. );
  45. $this->_systemAttribute = new \Magento\Framework\DataObject(
  46. ['is_user_defined' => false, 'attribute_code' => 'attribute_invisible_system', 'is_visible' => false]
  47. );
  48. $this->_attributes = [$this->_userAttribute, $this->_systemAttribute];
  49. $this->_model->expects(
  50. $this->any()
  51. )->method(
  52. '_getFilteredFormAttributeCollection'
  53. )->will(
  54. $this->returnValue($this->_attributes)
  55. );
  56. $this->_entity = new \Magento\Framework\DataObject(['id' => 1, 'attribute_visible_user' => 'abc']);
  57. $this->_model->expects($this->any())->method('getEntity')->will($this->returnValue($this->_entity));
  58. }
  59. /**
  60. * Unset form
  61. */
  62. protected function tearDown()
  63. {
  64. unset($this->_model);
  65. }
  66. /**
  67. * Test getAttributes
  68. */
  69. public function testGetAttributes()
  70. {
  71. $expected = [
  72. 'attribute_visible_user' => $this->_userAttribute,
  73. 'attribute_invisible_system' => $this->_systemAttribute,
  74. ];
  75. $this->assertEquals($expected, $this->_model->getAttributes());
  76. }
  77. /**
  78. * Test getUserAttributes
  79. */
  80. public function testGetUserAttributes()
  81. {
  82. $expected = ['attribute_visible_user' => $this->_userAttribute];
  83. $this->assertEquals($expected, $this->_model->getUserAttributes());
  84. }
  85. /**
  86. * Test getSystemAttributes
  87. */
  88. public function testGetSystemAttributes()
  89. {
  90. $expected = ['attribute_invisible_system' => $this->_systemAttribute];
  91. $this->assertEquals($expected, $this->_model->getSystemAttributes());
  92. }
  93. /**
  94. * Test getAllowedAttributes
  95. */
  96. public function testGetAllowedAttributes()
  97. {
  98. $expected = ['attribute_visible_user' => $this->_userAttribute];
  99. $this->assertEquals($expected, $this->_model->getAllowedAttributes());
  100. }
  101. /**
  102. * Test validateData method
  103. *
  104. * @dataProvider validateDataProvider
  105. *
  106. * @param bool $isValid
  107. * @param bool|array $expected
  108. * @param null|array $messages
  109. */
  110. public function testValidateDataPassed($isValid, $expected, $messages = null)
  111. {
  112. $validator = $this->getMockBuilder(
  113. \Magento\Eav\Model\Validator\Attribute\Data::class
  114. )->disableOriginalConstructor()->setMethods(
  115. ['isValid', 'getMessages']
  116. )->getMock();
  117. $validator->expects($this->once())->method('isValid')->will($this->returnValue($isValid));
  118. if ($messages) {
  119. $validator->expects($this->once())->method('getMessages')->will($this->returnValue($messages));
  120. } else {
  121. $validator->expects($this->never())->method('getMessages');
  122. }
  123. $this->_model->expects($this->once())->method('_getValidator')->will($this->returnValue($validator));
  124. $data = ['test' => true];
  125. $this->assertEquals($expected, $this->_model->validateData($data));
  126. }
  127. /**
  128. * Data provider for testValidateDataPassed
  129. *
  130. * @return array
  131. */
  132. public function validateDataProvider()
  133. {
  134. return [
  135. 'is_valid' => [true, true, null],
  136. 'is_invalid' => [false, ['Invalid'], ['attribute_visible_user' => ['Invalid']]]
  137. ];
  138. }
  139. }