Skip to content

Commit

Permalink
Merge branch '2.1-develop' of github.com:magento/magento2ce into pr-2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Shvets committed Oct 18, 2016
2 parents d42139b + 77ac059 commit 8b82897
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2211,12 +2211,17 @@ public function addMediaGalleryData()

$mediaGalleries = [];
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
$items = $this->getItems();

$select->where('entity.' . $linkField . ' IN (?)', array_map(function ($item) {
return $item->getId();
}, $items));

foreach ($this->getConnection()->fetchAll($select) as $row) {
$mediaGalleries[$row[$linkField]][] = $row;
}

foreach ($this->getItems() as $item) {
foreach ($items as $item) {
$mediaEntries = isset($mediaGalleries[$item->getId()]) ? $mediaGalleries[$item->getId()] : [];
$this->getGalleryReadHandler()->addMediaDataToProduct($item, $mediaEntries);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ class CollectionTest extends \PHPUnit_Framework_TestCase
*/
protected $collection;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $galleryResourceMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $entityMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $metadataPoolMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $galleryReadHandlerMock;

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
Expand Down Expand Up @@ -98,25 +118,49 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$entityMock = $this->getMockBuilder('Magento\Eav\Model\Entity\AbstractEntity')
$this->entityMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\AbstractEntity::class)
->disableOriginalConstructor()
->getMock();

$this->galleryResourceMock = $this->getMockBuilder(
\Magento\Catalog\Model\ResourceModel\Product\Gallery::class
)->disableOriginalConstructor()->getMock();

$this->metadataPoolMock = $this->getMockBuilder(
\Magento\Framework\EntityManager\MetadataPool::class
)->disableOriginalConstructor()->getMock();

$this->galleryReadHandlerMock = $this->getMockBuilder(
\Magento\Catalog\Model\Product\Gallery\ReadHandler::class
)->disableOriginalConstructor()->getMock();

$storeManager->expects($this->any())->method('getId')->willReturn(1);
$storeManager->expects($this->any())->method('getStore')->willReturnSelf();
$universalFactory->expects($this->exactly(1))->method('create')->willReturnOnConsecutiveCalls(
$entityMock
$this->entityMock
);
$entityMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
$entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn([]);
$entityMock->expects($this->any())->method('getTable')->willReturnArgument(0);
$this->entityMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
$this->entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn([]);
$this->entityMock->expects($this->any())->method('getTable')->willReturnArgument(0);
$this->connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($this->selectMock);
$helper = new ObjectManager($this);

$this->prepareObjectManager([
[
'Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation',
$this->getMock('Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation')
],
[
\Magento\Catalog\Model\ResourceModel\Product\Gallery::class,
$this->galleryResourceMock
],
[
\Magento\Framework\EntityManager\MetadataPool::class,
$this->metadataPoolMock
],
[
\Magento\Catalog\Model\Product\Gallery\ReadHandler::class,
$this->galleryReadHandlerMock
]
]);
$this->collection = $helper->getObject(
Expand Down Expand Up @@ -173,6 +217,47 @@ public function testAddProductCategoriesFilter()
$this->collection->addCategoriesFilter([$conditionType => $values]);
}

public function testAddMediaGalleryData()
{
$attributeId = 42;
$itemId = 4242;
$linkField = 'entity_id';
$mediaGalleriesMock = [[$linkField => $itemId]];
$itemMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->getMock();
$attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
->disableOriginalConstructor()
->getMock();
$selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
->disableOriginalConstructor()
->getMock();
$metadataMock = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
->disableOriginalConstructor()
->getMock();
$this->collection->addItem($itemMock);
$reflection = new \ReflectionClass(get_class($this->collection));
$reflectionProperty = $reflection->getProperty('_isCollectionLoaded');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->collection, true);

$this->galleryResourceMock->expects($this->once())->method('createBatchBaseSelect')->willReturn($selectMock);
$attributeMock->expects($this->once())->method('getAttributeId')->willReturn($attributeId);
$this->entityMock->expects($this->once())->method('getAttribute')->willReturn($attributeMock);
$itemMock->expects($this->atLeastOnce())->method('getId')->willReturn($itemId);
$selectMock->expects($this->once())->method('where')->with('entity.' . $linkField . ' IN (?)', [$itemId]);
$this->metadataPoolMock->expects($this->once())->method('getMetadata')->willReturn($metadataMock);
$metadataMock->expects($this->once())->method('getLinkField')->willReturn($linkField);

$this->connectionMock->expects($this->once())->method('fetchAll')->with($selectMock)->willReturn(
[['entity_id' => $itemId]]
);
$this->galleryReadHandlerMock->expects($this->once())->method('addMediaDataToProduct')
->with($itemMock, $mediaGalleriesMock);

$this->assertSame($this->collection, $this->collection->addMediaGalleryData());
}

/**
* @param $map
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Magento\Framework\Validator\Locale;
use Magento\Deploy\Console\Command\DeployStaticOptionsInterface as Options;
use Magento\Deploy\Model\DeployManager;
use Magento\Framework\App\Cache;
use Magento\Framework\App\Cache\Type\Dummy as DummyCache;

/**
* Deploy static content command
Expand Down Expand Up @@ -380,6 +382,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

$this->mockCache();
return $deployManager->deploy();
}

Expand Down Expand Up @@ -441,4 +444,18 @@ private function prepareDeployableEntities($filesUtil)

return [$deployableLanguages, $deployableAreaThemeMap, $requestedThemes];
}

/**
* Mock Cache class with dummy implementation
*
* @return void
*/
private function mockCache()
{
$this->objectManager->configure([
'preferences' => [
Cache::class => DummyCache::class
]
]);
}
}
40 changes: 37 additions & 3 deletions dev/tests/static/get_github_changes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// @codingStandardsIgnoreFile

define(
'USAGE',
<<<USAGE
php -f get_github_changes.php --
'USAGE',
<<<USAGE
php -f get_github_changes.php --
--output-file="<output_file>"
--base-path="<base_path>"
--repo="<main_repo>"
Expand All @@ -36,6 +36,8 @@

$mainline = 'mainline_' . (string)rand(0, 9999);
$repo = getRepo($options, $mainline);
$branches = $repo->getBranches('--remotes');
generateBranchesList($options['output-file'], $branches, $options['branch']);
$changes = retrieveChangesAcrossForks($mainline, $repo, $options['branch']);
$changedFiles = getChangedFiles($changes, $fileExtensions);
generateChangedFilesList($options['output-file'], $changedFiles);
Expand All @@ -57,6 +59,25 @@ function generateChangedFilesList($outputFile, $changedFiles)
fclose($changedFilesList);
}

/**
* Generates a file containing origin branches
*
* @param string $outputFile
* @param array $branches
* @param string $branchName
* @return void
*/
function generateBranchesList($outputFile, $branches, $branchName)
{
$branchOutputFile = str_replace('changed_files', 'branches', $outputFile);
$branchesList = fopen($branchOutputFile, 'w');
fwrite($branchesList, $branchName . PHP_EOL);
foreach ($branches as $branch) {
fwrite($branchesList, substr(strrchr($branch, '/'), 1) . PHP_EOL);
}
fclose($branchesList);
}

/**
* Gets list of changed files
*
Expand Down Expand Up @@ -203,6 +224,19 @@ public function fetch($remoteAlias)
$this->call(sprintf('fetch %s', $remoteAlias));
}

/**
* Returns repo branches
*
* @param string $source
* @return array|mixed
*/
public function getBranches($source = '--all')
{
$result = $this->call(sprintf('branch ' . $source));

return is_array($result) ? $result : [];
}

/**
* Returns files changes between branch and HEAD
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,46 @@ class ModuleDBChangeTest extends \PHPUnit_Framework_TestCase
/**
* @var string
*/
protected static $changedFilesPattern = __DIR__ . '/../_files/changed_files*';
private static $branchesFilesPattern = __DIR__ . '/../_files/branches*';

/**
* @var string
*/
protected static $changedFileList = '';
private static $changedFilesPattern = __DIR__ . '/../_files/changed_files*';

/**
* @var string
*/
private static $changedFileList = '';

/**
* @var bool
*/
private static $actualBranch = false;

/**
* Set changed files paths and list for all projects
*/
public static function setUpBeforeClass()
{
foreach (glob(self::$branchesFilesPattern) as $branchesFile) {
//get the current branchname from the first line
$branchName = trim(file($branchesFile)[0]);
if ($branchName === 'develop') {
self::$actualBranch = true;
} else {
//get current minor branch name
preg_match('|^(\d+\.\d+)|', $branchName, $minorBranch);
$branchName = $minorBranch[0];

//get all version branches
preg_match_all('|^(\d+\.\d+)|m', file_get_contents($branchesFile), $matches);

//check is this the latest release branch
self::$actualBranch = ($branchName == max($matches[0]));
}
}

foreach (glob(self::$changedFilesPattern) as $changedFile) {
self::$changedFileList .= file_get_contents($changedFile) . PHP_EOL;
}
Expand All @@ -37,24 +65,28 @@ public static function setUpBeforeClass()
*/
public function testModuleXmlFiles()
{
preg_match_all('|etc/module\.xml$|mi', self::$changedFileList, $matches);
$this->assertEmpty(
reset($matches),
'module.xml changes for patch releases in non-actual branches are not allowed:' . PHP_EOL .
implode(PHP_EOL, array_values(reset($matches)))
);
if (!self::$actualBranch) {
preg_match_all('|etc/module\.xml$|mi', self::$changedFileList, $matches);
$this->assertEmpty(
reset($matches),
'module.xml changes for patch releases in non-actual branches are not allowed:' . PHP_EOL .
implode(PHP_EOL, array_values(reset($matches)))
);
}
}

/**
* Test changes for files in Module Setup dir
*/
public function testModuleSetupFiles()
{
preg_match_all('|app/code/Magento/[^/]+/Setup/[^/]+$|mi', self::$changedFileList, $matches);
$this->assertEmpty(
reset($matches),
'Code with changes for DB schema or data in non-actual branches are not allowed:' . PHP_EOL .
implode(PHP_EOL, array_values(reset($matches)))
);
if (!self::$actualBranch) {
preg_match_all('|app/code/Magento/[^/]+/Setup/[^/]+$|mi', self::$changedFileList, $matches);
$this->assertEmpty(
reset($matches),
'Code with changes for DB schema or data in non-actual branches are not allowed:' . PHP_EOL .
implode(PHP_EOL, array_values(reset($matches)))
);
}
}
}
Loading

0 comments on commit 8b82897

Please sign in to comment.