123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Customer\Test\Unit\Block\Widget;
- use Magento\Customer\Api\Data\AttributeMetadataInterface;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Customer\Block\Widget\Name;
- /**
- * Test class for \Magento\Customer\Block\Widget\Name.
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class NameTest extends \PHPUnit\Framework\TestCase
- {
- /**#@+
- * Constant values used throughout the various unit tests.
- */
- const PREFIX = 'Mr';
- const MIDDLENAME = 'Middle';
- const SUFFIX = 'Jr';
- const KEY_CLASS_NAME = 'class_name';
- const DEFAULT_CLASS_NAME = 'customer-name';
- const CUSTOM_CLASS_NAME = 'my-class-name';
- const CONTAINER_CLASS_NAME_PREFIX = '-prefix';
- const CONTAINER_CLASS_NAME_MIDDLENAME = '-middlename';
- const CONTAINER_CLASS_NAME_SUFFIX = '-suffix';
- const PREFIX_ATTRIBUTE_CODE = 'prefix';
- const INVALID_ATTRIBUTE_CODE = 'invalid attribute code';
- const PREFIX_STORE_LABEL = 'Name Prefix';
- /**#@-*/
- /** @var \PHPUnit_Framework_MockObject_MockObject | AttributeMetadataInterface */
- private $attribute;
- /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Model\Options */
- private $_options;
- /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Escaper */
- private $_escaper;
- /** @var Name */
- private $_block;
- /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Customer\Api\CustomerMetadataInterface */
- private $customerMetadata;
- /** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Api\AddressMetadataInterface */
- private $addressMetadata;
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $_objectManager;
- protected function setUp()
- {
- $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->_escaper = $this->createMock(\Magento\Framework\Escaper::class);
- $context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
- $context->expects($this->any())->method('getEscaper')->will($this->returnValue($this->_escaper));
- $addressHelper = $this->createMock(\Magento\Customer\Helper\Address::class);
- $this->_options = $this->createMock(\Magento\Customer\Model\Options::class);
- $this->attribute = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
- ->getMockForAbstractClass();
- $this->customerMetadata = $this->getMockBuilder(\Magento\Customer\Api\CustomerMetadataInterface::class)
- ->getMockForAbstractClass();
- $this->customerMetadata->expects($this->any())
- ->method('getAttributeMetadata')
- ->will($this->returnValue($this->attribute));
- $this->customerMetadata
- ->expects($this->any())
- ->method('getCustomAttributesMetadata')
- ->will($this->returnValue([]));
- $this->addressMetadata = $this->getMockBuilder(\Magento\Customer\Api\AddressMetadataInterface::class)
- ->getMockForAbstractClass();
- $this->addressMetadata->expects($this->any())
- ->method('getAttributeMetadata')
- ->will($this->returnValue($this->attribute));
- $this->_block = new \Magento\Customer\Block\Widget\Name(
- $context,
- $addressHelper,
- $this->customerMetadata,
- $this->_options,
- $this->addressMetadata
- );
- }
- /**
- * @see self::_setUpShowAttribute()
- */
- public function testShowPrefix()
- {
- $this->_setUpShowAttribute([\Magento\Customer\Model\Data\Customer::PREFIX => self::PREFIX]);
- $this->assertTrue($this->_block->showPrefix());
- $this->attribute->expects($this->at(0))->method('isVisible')->will($this->returnValue(false));
- $this->assertFalse($this->_block->showPrefix());
- }
- public function testShowPrefixWithException()
- {
- $this->customerMetadata->expects(
- $this->any()
- )->method(
- 'getAttributeMetadata'
- )->will(
- $this->throwException(new NoSuchEntityException(
- __(
- 'No such entity with %fieldName = %fieldValue',
- ['fieldName' => 'field', 'fieldValue' => 'value']
- )
- ))
- );
- $this->assertFalse($this->_block->showPrefix());
- }
- /**
- * @param $method
- * @dataProvider methodDataProvider
- */
- public function testMethodWithNoSuchEntityException($method)
- {
- $this->customerMetadata->expects(
- $this->any()
- )->method(
- 'getAttributeMetadata'
- )->will(
- $this->throwException(new NoSuchEntityException(
- __(
- 'No such entity with %fieldName = %fieldValue',
- ['fieldName' => 'field', 'fieldValue' => 'value']
- )
- ))
- );
- $this->assertFalse($this->_block->{$method}());
- }
- /**
- * @return array
- */
- public function methodDataProvider()
- {
- return [
- 'showPrefix' => ['showPrefix'],
- 'isPrefixRequired' => ['isPrefixRequired'],
- 'showMiddlename' => ['showMiddlename'],
- 'isMiddlenameRequired' => ['isMiddlenameRequired'],
- 'showSuffix' => ['showSuffix'],
- 'isSuffixRequired' => ['isSuffixRequired']
- ];
- }
- /**
- * @see self::_setUpIsAttributeRequired()
- */
- public function testIsPrefixRequired()
- {
- $this->_setUpIsAttributeRequired();
- $this->assertTrue($this->_block->isPrefixRequired());
- }
- public function testShowMiddlename()
- {
- $this->_setUpShowAttribute([\Magento\Customer\Model\Data\Customer::MIDDLENAME => self::MIDDLENAME]);
- $this->assertTrue($this->_block->showMiddlename());
- }
- public function testIsMiddlenameRequired()
- {
- $this->_setUpIsAttributeRequired();
- $this->assertTrue($this->_block->isMiddlenameRequired());
- }
- public function testShowSuffix()
- {
- $this->_setUpShowAttribute([\Magento\Customer\Model\Data\Customer::SUFFIX => self::SUFFIX]);
- $this->assertTrue($this->_block->showSuffix());
- }
- public function testIsSuffixRequired()
- {
- $this->_setUpIsAttributeRequired();
- $this->assertTrue($this->_block->isSuffixRequired());
- }
- public function testGetPrefixOptionsNotEmpty()
- {
- /**
- * Added some padding so that the trim() call on Customer::getPrefix() will remove it. Also added
- * special characters so that the escapeHtml() method returns a htmlspecialchars translated value.
- */
- $customer = $this->getMockBuilder(
- \Magento\Customer\Api\Data\CustomerInterface::class
- )->getMockForAbstractClass();
- $customer->expects($this->once())->method('getPrefix')->willReturn(' <' . self::PREFIX . '> ');
- $this->_block->setObject($customer);
- $prefixOptions = ['Mrs' => 'Mrs', 'Ms' => 'Ms', 'Miss' => 'Miss'];
- $prefix = '<' . self::PREFIX . '>';
- $expectedOptions = $prefixOptions;
- $expectedOptions[$prefix] = $prefix;
- $this->_options->expects(
- $this->once()
- )->method(
- 'getNamePrefixOptions'
- )->will(
- $this->returnValue($prefixOptions)
- );
- $this->_escaper->expects($this->once())->method('escapeHtml')->will($this->returnValue($prefix));
- $this->assertSame($expectedOptions, $this->_block->getPrefixOptions());
- }
- public function testGetPrefixOptionsEmpty()
- {
- $customer = $this->getMockBuilder(
- \Magento\Customer\Api\Data\CustomerInterface::class
- )->getMockForAbstractClass();
- $this->_block->setObject($customer);
- $this->_options->expects(
- $this->once()
- )->method(
- 'getNamePrefixOptions'
- )->will(
- $this->returnValue([])
- );
- $this->assertEmpty($this->_block->getPrefixOptions());
- }
- public function testGetSuffixOptionsNotEmpty()
- {
- /**
- * Added padding and special characters to show that trim() works on Customer::getSuffix() and that
- * a properly htmlspecialchars translated value is returned.
- */
- $customer = $this->getMockBuilder(
- \Magento\Customer\Api\Data\CustomerInterface::class
- )->getMockForAbstractClass();
- $customer->expects($this->once())->method('getSuffix')->willReturn(' <' . self::SUFFIX . '> ');
- $this->_block->setObject($customer);
- $suffixOptions = ['Sr' => 'Sr'];
- $suffix = '<' . self::SUFFIX . '>';
- $expectedOptions = $suffixOptions;
- $expectedOptions[$suffix] = $suffix;
- $this->_options->expects(
- $this->once()
- )->method(
- 'getNameSuffixOptions'
- )->will(
- $this->returnValue($suffixOptions)
- );
- $this->_escaper->expects($this->once())->method('escapeHtml')->will($this->returnValue($suffix));
- $this->assertSame($expectedOptions, $this->_block->getSuffixOptions());
- }
- public function testGetSuffixOptionsEmpty()
- {
- $customer = $this->getMockBuilder(
- \Magento\Customer\Api\Data\CustomerInterface::class
- )->getMockForAbstractClass();
- $this->_block->setObject($customer);
- $this->_options->expects(
- $this->once()
- )->method(
- 'getNameSuffixOptions'
- )->will(
- $this->returnValue([])
- );
- $this->assertEmpty($this->_block->getSuffixOptions());
- }
- public function testGetClassName()
- {
- /** Test the default case when the block has no data set for the class name. */
- $this->assertEquals(self::DEFAULT_CLASS_NAME, $this->_block->getClassName());
- /** Set custom data for the class name and verify that the Name::getClassName() method returns it. */
- $this->_block->setData(self::KEY_CLASS_NAME, self::CUSTOM_CLASS_NAME);
- $this->assertEquals(self::CUSTOM_CLASS_NAME, $this->_block->getClassName());
- }
- /**
- * @param bool $isPrefixVisible Value returned by Name::showPrefix()
- * @param bool $isMiddlenameVisible Value returned by Name::showMiddlename()
- * @param bool $isSuffixVisible Value returned by Name::showSuffix()
- * @param string $expectedValue The expected value of Name::getContainerClassName()
- *
- * @dataProvider getContainerClassNameProvider
- */
- public function testGetContainerClassName($isPrefixVisible, $isMiddlenameVisible, $isSuffixVisible, $expectedValue)
- {
- $this->attribute->expects(
- $this->at(0)
- )->method(
- 'isVisible'
- )->will(
- $this->returnValue($isPrefixVisible)
- );
- $this->attribute->expects(
- $this->at(1)
- )->method(
- 'isVisible'
- )->will(
- $this->returnValue($isMiddlenameVisible)
- );
- $this->attribute->expects(
- $this->at(2)
- )->method(
- 'isVisible'
- )->will(
- $this->returnValue($isSuffixVisible)
- );
- $this->assertEquals($expectedValue, $this->_block->getContainerClassName());
- }
- /**
- * This data provider provides enough data sets to test both ternary operator code paths for each one
- * that's used in Name::getContainerClassName().
- *
- * @return array
- */
- public function getContainerClassNameProvider()
- {
- return [
- [false, false, false, self::DEFAULT_CLASS_NAME],
- [true, false, false, self::DEFAULT_CLASS_NAME . self::CONTAINER_CLASS_NAME_PREFIX],
- [false, true, false, self::DEFAULT_CLASS_NAME . self::CONTAINER_CLASS_NAME_MIDDLENAME],
- [false, false, true, self::DEFAULT_CLASS_NAME . self::CONTAINER_CLASS_NAME_SUFFIX],
- [
- true,
- true,
- true,
- self::DEFAULT_CLASS_NAME .
- self::CONTAINER_CLASS_NAME_PREFIX .
- self::CONTAINER_CLASS_NAME_MIDDLENAME .
- self::CONTAINER_CLASS_NAME_SUFFIX
- ]
- ];
- }
- /**
- * @param string $attributeCode An attribute code
- * @param string $storeLabel The attribute's store label
- * @param string $expectedValue The expected value of Name::getStoreLabel()
- *
- * @dataProvider getStoreLabelProvider
- */
- public function testGetStoreLabel($attributeCode, $storeLabel, $expectedValue)
- {
- $this->attribute->expects($this->atLeastOnce())->method('getStoreLabel')->willReturn($storeLabel);
- $this->assertEquals($expectedValue, $this->_block->getStoreLabel($attributeCode));
- }
- /**
- * This data provider provides two data sets. One tests that an empty string is returned for an invalid
- * attribute code instead of an exception being thrown. The second tests that the correct store label is
- * returned for a valid attribute code.
- *
- * @return array
- */
- public function getStoreLabelProvider()
- {
- return [
- [self::INVALID_ATTRIBUTE_CODE, '', ''],
- [self::PREFIX_ATTRIBUTE_CODE, self::PREFIX_STORE_LABEL, self::PREFIX_STORE_LABEL]
- ];
- }
- public function testGetStoreLabelWithException()
- {
- $this->customerMetadata->expects(
- $this->any()
- )->method(
- 'getAttributeMetadata'
- )->will(
- $this->throwException(new NoSuchEntityException(
- __(
- 'No such entity with %fieldName = %fieldValue',
- ['fieldName' => 'field', 'fieldValue' => 'value']
- )
- ))
- );
- $this->assertSame('', (string)$this->_block->getStoreLabel('attributeCode'));
- }
- /**
- * Helper method for testing all show*() methods.
- *
- * @param array $data Customer attribute(s)
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- private function _setUpShowAttribute(array $data)
- {
- $customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
- ->getMockForAbstractClass();
- /**
- * These settings cause the first code path in Name::_getAttribute() to be executed, which
- * basically just returns the value of parent::_getAttribute().
- */
- $this->_block->setForceUseCustomerAttributes(true);
- $this->_block->setObject($customer);
- /**
- * The show*() methods return true for the attribute returned by parent::_getAttribute() for the
- * first call to the method. Subsequent calls may return true or false depending on the returnValue
- * of the at({0, 1, 2, 3, ...}), etc. calls as set and configured in a particular test.
- */
- $this->attribute->expects($this->at(0))->method('isVisible')->will($this->returnValue(true));
- }
- /**
- * Helper method for testing all is*Required() methods.
- */
- private function _setUpIsAttributeRequired()
- {
- /**
- * These settings cause the first code path in Name::_getAttribute() to be skipped so that the rest of
- * the code in the other code path(s) can be executed.
- */
- $this->_block->setForceUseCustomerAttributes(false);
- $this->_block->setForceUseCustomerRequiredAttributes(true);
- $this->_block->setObject(new \StdClass());
- /**
- * The first call to isRequired() is false so that the second if conditional in the other code path
- * of Name::_getAttribute() will evaluate to true, which causes the if's code block to be executed.
- * The second isRequired() call causes the code in the nested if conditional to be executed. Thus,
- * all code paths in Name::_getAttribute() will be executed. Returning true for the third isRequired()
- * call causes the is*Required() method of the block to return true for the attribute.
- */
- $this->attribute->expects($this->at(0))->method('isRequired')->will($this->returnValue(false));
- $this->attribute->expects($this->at(1))->method('isRequired')->will($this->returnValue(true));
- $this->attribute->expects($this->at(2))->method('isRequired')->will($this->returnValue(true));
- }
- }
|