26 private $resourceMock;
31 private $resourceCollectionMock;
36 private $validationFailedPhrase;
41 private $objectManager;
46 $this->escaperMock = $this->getMockBuilder(\
Magento\Framework\Escaper::class)
47 ->disableOriginalConstructor()
49 $this->resourceMock = $this->getMockBuilder(\
Magento\Variable\Model\
ResourceModel\Variable::class)
50 ->disableOriginalConstructor()
52 $this->resourceCollectionMock = $this->getMockBuilder(Collection::class)
53 ->disableOriginalConstructor()
55 $this->model = $this->objectManager->getObject(
56 \
Magento\Variable\Model\Variable::class,
58 'escaper' => $this->escaperMock,
59 'resource' => $this->resourceMock,
60 'resourceCollection' => $this->resourceCollectionMock,
63 $this->validationFailedPhrase =
__(
'Validation has failed.');
70 $this->model->setData(
'html_value', $html);
71 $this->assertSame($html, $this->model->getValue(
$type));
78 $plain =
'unescaped_plain_text';
79 $escapedPlain =
'escaped_plain_text';
80 $this->model->setData(
'html_value', $html);
81 $this->model->setData(
'plain_value', $plain);
82 $this->escaperMock->expects($this->once())
83 ->method(
'escapeHtml')
85 ->willReturn($escapedPlain);
86 $this->assertSame($escapedPlain, $this->model->getValue(
$type));
93 $this->model->setData(
'plain_value', $plain);
94 $this->assertSame($plain, $this->model->getValue(
$type));
103 $this->assertEquals($this->validationFailedPhrase, $this->model->validate());
109 public function testValidate($variableArray, $objectId, $expectedResult)
111 $code =
'variable_code';
112 $this->model->setCode(
$code)->setName(
'some_name');
113 $this->resourceMock->expects($this->once())
114 ->method(
'getVariableByCode')
116 ->willReturn($variableArray);
117 $this->model->setId($objectId);
118 $this->assertEquals($expectedResult, $this->model->validate($variableArray));
124 [
'value' =>
'VAL',
'label' =>
'LBL'],
127 $transformedOptions = [
128 [
'value' =>
'{{customVar code=VAL}}',
'label' =>
__(
'%1',
'LBL')],
131 $this->resourceCollectionMock->expects($this->any())
132 ->method(
'toOptionArray')
133 ->willReturn($origOptions);
134 $this->escaperMock->expects($this->once())
135 ->method(
'escapeHtml')
136 ->with($origOptions[0][
'label'])
137 ->willReturn($origOptions[0][
'label']);
138 $this->assertEquals($transformedOptions, $this->model->getVariablesOptionArray());
144 [
'value' =>
'VAL',
'label' =>
'LBL'],
147 $transformedOptions = [
149 'label' =>
__(
'Custom Variables'),
151 [
'value' =>
'{{customVar code=VAL}}',
'label' =>
__(
'%1',
'LBL')],
156 $this->resourceCollectionMock->expects($this->any())
157 ->method(
'toOptionArray')
158 ->willReturn($origOptions);
159 $this->escaperMock->expects($this->atLeastOnce())
160 ->method(
'escapeHtml')
161 ->with($origOptions[0][
'label'])
162 ->willReturn($origOptions[0][
'label']);
163 $this->assertEquals($transformedOptions, $this->model->getVariablesOptionArray(
true));
172 'variable_id' =>
'matching_id',
175 'Empty Variable' => [[],
null,
true],
176 'IDs match' => [
$variable,
'matching_id',
true],
177 'IDs do not match' => [
$variable,
'non_matching_id',
__(
'Variable Code must be unique.')],
187 'Missing code' => [
'',
'some-name'],
188 'Missing name' => [
'some-code',
''],
validateMissingInfoDataProvider()
testValidateMissingInfo($code, $name)
testGetVariablesOptionArrayNoGroup()
testGetVariablesOptionArrayWithGroup()
testValidate($variableArray, $objectId, $expectedResult)
if(!isset($_GET['name'])) $name