/** * Set translator to chain. * * @param \Magento\Framework\Translate\AdapterInterface|null $translator * @return \Magento\Framework\Validator\AbstractValidator */ public function setTranslator($translator = null) { foreach ($this->_validators as $validator) { $validator['instance']->setTranslator($translator); } return parent::setTranslator($translator); }
/** * Data provider for * * @return array */ public function createValidatorDataProvider() { $result = array(); /** @var \Magento\Framework\Translate\AbstractAdapter $translator */ $translator = $this->getMockBuilder('Magento\\Framework\\Translate\\AbstractAdapter')->getMockForAbstractClass(); \Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($translator); // Case 1. Check constructor with arguments $actualConstraints = array(array('alias' => 'name_alias', 'class' => 'Magento\\Framework\\Validator\\Test\\StringLength', 'options' => array('arguments' => array('options' => array('min' => 1, 'max' => new \Magento\Framework\Validator\Constraint\Option(20)))), 'property' => 'name', 'type' => 'property')); $expectedValidator = new \Magento\Framework\Validator(); $expectedValidator->addValidator(new \Magento\Framework\Validator\Constraint\Property(new \Magento\Framework\Validator\Test\StringLength(1, 20), 'name', 'name_alias')); $result[] = array($actualConstraints, $expectedValidator); // Case 2. Check method calls $actualConstraints = array(array('alias' => 'description_alias', 'class' => 'Magento\\Framework\\Validator\\Test\\StringLength', 'options' => array('methods' => array(array('method' => 'setMin', 'arguments' => array(10)), array('method' => 'setMax', 'arguments' => array(1000)))), 'property' => 'description', 'type' => 'property')); $expectedValidator = new \Magento\Framework\Validator(); $expectedValidator->addValidator(new \Magento\Framework\Validator\Constraint\Property(new \Magento\Framework\Validator\Test\StringLength(10, 1000), 'description', 'description_alias')); $result[] = array($actualConstraints, $expectedValidator); // Case 3. Check callback on validator $actualConstraints = array(array('alias' => 'sku_alias', 'class' => 'Magento\\Framework\\Validator\\Test\\StringLength', 'options' => array('callback' => array(new \Magento\Framework\Validator\Constraint\Option\Callback(function ($validator) { $validator->setMin(20); $validator->setMax(100); }))), 'property' => 'sku', 'type' => 'property')); $expectedValidator = new \Magento\Framework\Validator(); $expectedValidator->addValidator(new \Magento\Framework\Validator\Constraint\Property(new \Magento\Framework\Validator\Test\StringLength(20, 100), 'sku', 'sku_alias')); $result[] = array($actualConstraints, $expectedValidator); return $result; }
/** * Test getValidatorConfig created correct validator config. Check that validator translator was initialized. */ public function testGetValidatorConfig() { $factory = new \Magento\Core\Model\Validator\Factory($this->_objectManager, $this->_config, $this->_translateAdapter); $actualConfig = $factory->getValidatorConfig(); $this->assertInstanceOf('Magento\\Framework\\Validator\\Config', $actualConfig, 'Object of incorrect type was created'); // Check that validator translator was correctly instantiated $validatorTranslator = \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(); $this->assertInstanceOf('Magento\\Framework\\Translate\\Adapter', $validatorTranslator, 'Default validator translate adapter was not set correctly'); }
/** * Check that default translator returned if set and no translator set */ public function testGetTranslatorDefault() { /** @var \Magento\Framework\Validator\AbstractValidator $validator */ $validator = $this->getMockBuilder('Magento\\Framework\\Validator\\AbstractValidator')->getMockForAbstractClass(); $translator = $this->_getTranslator(); \Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($translator); $this->assertEquals($translator, $validator->getTranslator()); $this->assertFalse($validator->hasTranslator()); }
/** * Create and set default translator to \Magento\Framework\Validator\AbstractValidator. * * @return void */ protected function _initializeDefaultTranslator() { // Pass translations to \Magento\Framework\TranslateInterface from validators $translatorCallback = function () { $argc = func_get_args(); return (string) new \Magento\Framework\Phrase(array_shift($argc), $argc); }; /** @var \Magento\Framework\Translate\Adapter $translator */ $translator = $this->_objectManager->create('Magento\\Framework\\Translate\\Adapter'); $translator->setOptions(['translator' => $translatorCallback]); \Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($translator); }
/** * Test creation of validator config * * @magentoAppIsolation enabled */ public function testGetValidatorConfig() { $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); /** @var \Magento\Framework\Validator\Factory $factory */ $factory = $objectManager->get('Magento\\Framework\\Validator\\Factory'); $this->assertInstanceOf('Magento\\Framework\\Validator\\Config', $factory->getValidatorConfig()); // Check that default translator was set $translator = \Magento\Framework\Validator\AbstractValidator::getDefaultTranslator(); $this->assertInstanceOf('Magento\\Framework\\Translate\\AdapterInterface', $translator); $this->assertEquals('Message', new \Magento\Framework\Phrase('Message')); $this->assertEquals('Message', $translator->translate('Message')); $this->assertEquals('Message with "placeholder one" and "placeholder two"', (string) new \Magento\Framework\Phrase('Message with "%1" and "%2"', ['placeholder one', 'placeholder two'])); }
/** * Test addValidator */ public function testAddValidator() { $fooValidator = new \Magento\Framework\Validator\Test\Unit\Test\IsTrue(); $classConstraint = new \Magento\Framework\Validator\Constraint($fooValidator, 'id'); $propertyValidator = new \Magento\Framework\Validator\Constraint\Property($classConstraint, 'name', 'id'); /** @var \Magento\Framework\Translate\AbstractAdapter $translator */ $translator = $this->getMockBuilder('Magento\\Framework\\Translate\\AbstractAdapter')->getMockForAbstractClass(); \Magento\Framework\Validator\AbstractValidator::setDefaultTranslator($translator); $this->_validator->addValidator($classConstraint); $this->_validator->addValidator($propertyValidator); $expected = [['instance' => $classConstraint, 'breakChainOnFailure' => false], ['instance' => $propertyValidator, 'breakChainOnFailure' => false]]; $this->assertAttributeEquals($expected, '_validators', $this->_validator); $this->assertEquals($translator, $fooValidator->getTranslator(), 'Translator was not set'); }