Skip to content

Commit bdfd8d2

Browse files
committed
MAGETWO-58072: [Backport] - [GITHUB] php bin/magento i18n:pack creates unwanted dir #6260 - for 2.1
1 parent 87046fa commit bdfd8d2

File tree

3 files changed

+245
-16
lines changed

3 files changed

+245
-16
lines changed

setup/src/Magento/Setup/Module/I18n/Context.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Setup\Module\I18n;
77

8-
use Magento\Framework\App\Filesystem\DirectoryList;
98
use Magento\Framework\Component\ComponentRegistrar;
109
use Magento\Framework\Filesystem;
1110

@@ -93,7 +92,7 @@ private function getComponentName($componentType, $path)
9392
*
9493
* @param string $type
9594
* @param array $value
96-
* @return string
95+
* @return string|null
9796
* @throws \InvalidArgumentException
9897
*/
9998
public function buildPathToLocaleDirectoryByContext($type, $value)
@@ -111,6 +110,7 @@ public function buildPathToLocaleDirectoryByContext($type, $value)
111110
default:
112111
throw new \InvalidArgumentException(sprintf('Invalid context given: "%s".', $type));
113112
}
114-
return $path . '/' . self::LOCALE_DIRECTORY . '/';
113+
114+
return (null === $path) ? null : $path . '/' . self::LOCALE_DIRECTORY . '/';
115115
}
116116
}

setup/src/Magento/Setup/Test/Unit/Module/I18n/ContextTest.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ class ContextTest extends \PHPUnit_Framework_TestCase
2222

2323
protected function setUp()
2424
{
25-
$this->componentRegistrar = $this->getMock('Magento\Framework\Component\ComponentRegistrar', [], [], '', false);
25+
$this->componentRegistrar = $this->getMock(
26+
\Magento\Framework\Component\ComponentRegistrar::class,
27+
[],
28+
[],
29+
'',
30+
false
31+
);
2632
}
2733

2834
/**
@@ -35,7 +41,7 @@ public function testGetContextByPath($context, $path, $pathValues)
3541
{
3642
$this->componentRegistrar->expects($this->any())
3743
->method('getPaths')
38-
->will($this->returnValueMap($pathValues));
44+
->willReturnMap($pathValues);
3945
$this->context = new Context($this->componentRegistrar);
4046
$this->assertEquals($context, $this->context->getContextByPath($path));
4147
}
@@ -102,10 +108,8 @@ public function testBuildPathToLocaleDirectoryByContext($path, $context, $regist
102108
$paths[$module[1]] = $module[2];
103109
}
104110
$this->componentRegistrar->expects($this->any())
105-
->method('getPaths')
106-
->with(ComponentRegistrar::MODULE)
107-
->willReturn($paths);
108-
$this->componentRegistrar->expects($this->any())->method('getPath')->will($this->returnValueMap($registrar));
111+
->method('getPath')
112+
->willReturnMap($registrar);
109113
$this->context = new Context($this->componentRegistrar);
110114
$this->assertEquals($path, $this->context->buildPathToLocaleDirectoryByContext($context[0], $context[1]));
111115
}
@@ -119,10 +123,29 @@ public function dataProviderPathToLocaleDirectoryByContext()
119123
[
120124
BP . '/app/code/Magento/Module/i18n/',
121125
[Context::CONTEXT_TYPE_MODULE, 'Magento_Module'],
122-
[[ComponentRegistrar::MODULE, 'Magento_Module', BP . '/app/code/Magento/Module']]
126+
[[ComponentRegistrar::MODULE, 'Magento_Module', BP . '/app/code/Magento/Module']],
127+
],
128+
[
129+
BP . '/app/design/frontend/Magento/luma/i18n/',
130+
[Context::CONTEXT_TYPE_THEME, 'frontend/Magento/luma'],
131+
[[ComponentRegistrar::THEME, 'frontend/Magento/luma', BP . '/app/design/frontend/Magento/luma']],
132+
],
133+
134+
[
135+
null,
136+
[Context::CONTEXT_TYPE_MODULE, 'Unregistered_Module'],
137+
[[ComponentRegistrar::MODULE, 'Unregistered_Module', null]],
138+
],
139+
[
140+
null,
141+
[Context::CONTEXT_TYPE_THEME, 'frontend/Magento/unregistered'],
142+
[[ComponentRegistrar::THEME, 'frontend/Magento/unregistered', null]],
143+
],
144+
[
145+
BP . '/lib/web/i18n/',
146+
[Context::CONTEXT_TYPE_LIB, 'lib/web/module/test.phtml'],
147+
[],
123148
],
124-
['/i18n/', [Context::CONTEXT_TYPE_THEME, 'theme/test.phtml'], []],
125-
[BP . '/lib/web/i18n/', [Context::CONTEXT_TYPE_LIB, 'lib/web/module/test.phtml'], []],
126149
];
127150
}
128151

@@ -132,10 +155,8 @@ public function dataProviderPathToLocaleDirectoryByContext()
132155
*/
133156
public function testBuildPathToLocaleDirectoryByContextWithInvalidType()
134157
{
135-
$this->componentRegistrar->expects($this->any())
136-
->method('getPaths')
137-
->with(ComponentRegistrar::MODULE)
138-
->willReturn(['module' => '/path/to/module']);
158+
$this->componentRegistrar->expects($this->never())
159+
->method('getPath');
139160
$this->context = new Context($this->componentRegistrar);
140161
$this->context->buildPathToLocaleDirectoryByContext('invalid_type', 'Magento_Module');
141162
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Setup\Test\Unit\Module\I18n\Pack\Writer\File;
7+
8+
use Magento\Setup\Module\I18n\Context;
9+
use Magento\Setup\Module\I18n\Locale;
10+
use Magento\Setup\Module\I18n\Dictionary;
11+
use Magento\Setup\Module\I18n\Dictionary\Phrase;
12+
use Magento\Setup\Module\I18n\Pack\Writer\File\AbstractFile;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
14+
15+
/**
16+
* Tests for Magento\Setup\Module\I18n\Pack\Writer\File\AbstractFile
17+
*/
18+
class AbstractFileTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $contextMock;
24+
25+
/**
26+
* @var Locale|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $localeMock;
29+
30+
/**
31+
* @var Dictionary|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $dictionaryMock;
34+
35+
/**
36+
* @var Phrase|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $phraseMock;
39+
40+
/**
41+
* @var AbstractFile|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $object;
44+
45+
/**
46+
* @inheritdoc
47+
*/
48+
protected function setUp()
49+
{
50+
$objectManagerHelper = new ObjectManagerHelper($this);
51+
52+
$this->contextMock = $this->getMock(Context::class, [], [], '', false, false);
53+
$this->localeMock = $this->getMock(Locale::class, [], [], '', false, false);
54+
$this->dictionaryMock = $this->getMock(Dictionary::class, [], [], '', false, false);
55+
$this->phraseMock = $this->getMock(Phrase::class, [], [], '', false, false);
56+
57+
$constructorArguments = $objectManagerHelper->getConstructArguments(
58+
AbstractFile::class,
59+
['context' => $this->contextMock]
60+
);
61+
62+
$this->object = $this->getMockBuilder(AbstractFile::class)
63+
->setMethods(['_createDirectoryIfNotExist', '_writeFile'])
64+
->setConstructorArgs($constructorArguments)
65+
->getMockForAbstractClass();
66+
}
67+
68+
/**
69+
* @param string $contextType
70+
* @param array $contextValue
71+
* @dataProvider writeDictionaryWithRuntimeExceptionDataProvider
72+
* @expectedException \RuntimeException
73+
* @return void
74+
*/
75+
public function testWriteDictionaryWithRuntimeException(
76+
$contextType,
77+
array $contextValue
78+
) {
79+
$this->configureGeneralPhrasesMock($contextType, $contextValue);
80+
81+
$this->object->expects($this->never())
82+
->method('_createDirectoryIfNotExist');
83+
$this->object->expects($this->never())
84+
->method('_writeFile');
85+
$this->object->writeDictionary($this->dictionaryMock, $this->localeMock);
86+
}
87+
88+
/**
89+
* @return array
90+
*/
91+
public function writeDictionaryWithRuntimeExceptionDataProvider()
92+
{
93+
return [
94+
['', []],
95+
['module', []],
96+
['', ['Magento_Module']],
97+
];
98+
}
99+
100+
/**
101+
* @expectedException \InvalidArgumentException
102+
* @expectedExceptionMessage Some error. Row #1.
103+
* @return void
104+
*/
105+
public function testWriteDictionaryWithInvalidArgumentException()
106+
{
107+
$contextType = 'module';
108+
$contextValue = 'Magento_Module';
109+
110+
$this->configureGeneralPhrasesMock($contextType, [$contextValue]);
111+
112+
$this->object->expects($this->never())
113+
->method('_createDirectoryIfNotExist');
114+
$this->object->expects($this->never())
115+
->method('_writeFile');
116+
117+
$this->contextMock->expects($this->once())
118+
->method('buildPathToLocaleDirectoryByContext')
119+
->with($contextType, $contextValue)
120+
->willThrowException(new \InvalidArgumentException('Some error.'));
121+
122+
$this->object->writeDictionary($this->dictionaryMock, $this->localeMock);
123+
}
124+
125+
/**
126+
* @return void
127+
*/
128+
public function testWriteDictionaryWherePathIsNull()
129+
{
130+
$contextType = 'module';
131+
$contextValue = 'Magento_Module';
132+
133+
$this->configureGeneralPhrasesMock($contextType, [$contextValue]);
134+
135+
$this->object->expects($this->never())
136+
->method('_createDirectoryIfNotExist');
137+
$this->object->expects($this->never())
138+
->method('_writeFile');
139+
140+
$this->contextMock->expects($this->once())
141+
->method('buildPathToLocaleDirectoryByContext')
142+
->with($contextType, $contextValue)
143+
->willReturn(null);
144+
145+
$this->object->writeDictionary($this->dictionaryMock, $this->localeMock);
146+
}
147+
148+
/**
149+
* @return void
150+
*/
151+
public function testWriteDictionary()
152+
{
153+
$contextType = 'module';
154+
$contextValue = 'Magento_Module';
155+
$path = '/some/path/';
156+
$phrase = 'Phrase';
157+
$locale = 'en_EN';
158+
$fileExtension = 'csv';
159+
$file = $path . $locale . '.' . $fileExtension;
160+
161+
$this->configureGeneralPhrasesMock($contextType, [$contextValue]);
162+
163+
$this->phraseMock->expects($this->once())
164+
->method('getPhrase')
165+
->willReturn($phrase);
166+
167+
$this->localeMock->expects($this->once())
168+
->method('__toString')
169+
->willReturn($locale);
170+
171+
$this->object->expects($this->once())
172+
->method('_getFileExtension')
173+
->willReturn($fileExtension);
174+
$this->object->expects($this->once())
175+
->method('_createDirectoryIfNotExist')
176+
->with(dirname($file));
177+
$this->object->expects($this->once())
178+
->method('_writeFile')
179+
->with($file, [$phrase => $this->phraseMock]);
180+
181+
$this->contextMock->expects($this->once())
182+
->method('buildPathToLocaleDirectoryByContext')
183+
->with($contextType, $contextValue)
184+
->willReturn($path);
185+
186+
$this->object->writeDictionary($this->dictionaryMock, $this->localeMock);
187+
}
188+
189+
/**
190+
* @param string $contextType
191+
* @param array $contextValue
192+
* @return void
193+
*/
194+
private function configureGeneralPhrasesMock($contextType, array $contextValue)
195+
{
196+
$this->phraseMock->expects($this->any())
197+
->method('getContextType')
198+
->willReturn($contextType);
199+
200+
$this->phraseMock->expects($this->any())
201+
->method('getContextValue')
202+
->willReturn($contextValue);
203+
204+
$this->dictionaryMock->expects($this->once())
205+
->method('getPhrases')
206+
->willReturn([$this->phraseMock]);
207+
}
208+
}

0 commit comments

Comments
 (0)