Skip to content

Commit db894ca

Browse files
committed
MC-20425: [Integration Test] Check behavior when attribute set was changed to a new set with deleted attribute from the previous set
1 parent edd8c13 commit db894ca

File tree

3 files changed

+35
-102
lines changed

3 files changed

+35
-102
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Controller/Product/ViewTest.php

Lines changed: 35 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@
77

88
namespace Magento\Catalog\Controller\Product;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Api\ProductRepositoryInterface;
1112
use Magento\Catalog\Model\Product;
1213
use Magento\Eav\Model\AttributeSetSearchResults;
1314
use Magento\Eav\Model\Entity\Attribute\Set;
1415
use Magento\Framework\Api\SearchCriteriaBuilder;
1516
use Magento\Framework\Api\SortOrderBuilder;
16-
use Magento\Framework\App\Filesystem\DirectoryList;
1717
use Magento\Framework\Data\Collection;
1818
use Magento\Catalog\Api\AttributeSetRepositoryInterface;
1919
use Magento\Eav\Model\Entity\Type;
20-
use Magento\Framework\Filesystem;
21-
use Magento\Framework\Filesystem\Directory\WriteInterface;
22-
use Magento\Framework\Filesystem\File\WriteInterface as FileWriteInterface;
23-
use Magento\Framework\Filesystem\Driver\File;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use Psr\Log\LoggerInterface;
22+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
23+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
24+
use Magento\Framework\Logger\Monolog as MagentoMonologLogger;
2425

2526
/**
2627
* Integration test for product view front action.
@@ -45,6 +46,11 @@ class ViewTest extends \Magento\TestFramework\TestCase\AbstractController
4546
*/
4647
private $attributeSetRepository;
4748

49+
/**
50+
* @var ProductAttributeRepositoryInterface $attributeSetRepository
51+
*/
52+
private $attributeRepository;
53+
4854
/**
4955
* @var Type $productEntityType
5056
*/
@@ -59,6 +65,7 @@ protected function setUp()
5965

6066
$this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
6167
$this->attributeSetRepository = $this->_objectManager->create(AttributeSetRepositoryInterface::class);
68+
$this->attributeRepository = $this->_objectManager->create(ProductAttributeRepositoryInterface::class);
6269
$this->productEntityType = $this->_objectManager->create(Type::class)
6370
->loadByCode(Product::ENTITY);
6471
}
@@ -94,32 +101,42 @@ public function testViewActionWithCanonicalTag(): void
94101
*/
95102
public function testViewActionCustomAttributeSetWithoutCountryOfManufacture(): void
96103
{
104+
/** @var MockObject|LoggerInterface $logger */
105+
$logger = $this->setupLoggerMock();
106+
97107
$product = $this->getProductBySku('simple_with_com');
98108
$attributeSetCustom = $this->getProductAttributeSetByName('custom_attribute_set_wout_com');
99-
100109
$product->setAttributeSetId($attributeSetCustom->getAttributeSetId());
101110
$this->productRepository->save($product);
102111

112+
/** @var ProductAttributeInterface $attributeCountryOfManufacture */
113+
$attributeCountryOfManufacture = $this->attributeRepository->get('country_of_manufacture');
114+
$logger->expects($this->never())
115+
->method('warning')
116+
->with(
117+
"Attempt to load value of nonexistent EAV attribute",
118+
[
119+
'attribute_id' => $attributeCountryOfManufacture->getAttributeId(),
120+
'entity_type' => ProductInterface::class,
121+
]
122+
);
123+
103124
$this->dispatch(sprintf('catalog/product/view/id/%s/', $product->getId()));
104-
$message = 'Attempt to load value of nonexistent EAV attribute';
105-
$this->assertFalse(
106-
$this->checkSystemLogForMessage($message),
107-
sprintf("Warning message found in %s: %s", $this->systemLogFileName, $message)
108-
);
109125
}
110126

111127
/**
112-
* Check system log file for error message.
128+
* Setup logger mock to check there are no warning messages logged.
113129
*
114-
* @param string $message
115-
* @return bool
130+
* @return MockObject
116131
*/
117-
private function checkSystemLogForMessage(string $message): bool
132+
private function setupLoggerMock() : MockObject
118133
{
119-
$content = $this->getSystemLogContent();
120-
$pos = strpos($content, $message);
134+
$logger = $this->getMockBuilder(LoggerInterface::class)
135+
->disableOriginalConstructor()
136+
->getMock();
137+
$this->_objectManager->addSharedInstance($logger, MagentoMonologLogger::class);
121138

122-
return $pos !== false;
139+
return $logger;
123140
}
124141

125142
/**
@@ -165,86 +182,4 @@ private function getProductAttributeSetByName(string $attributeSetName): ?Set
165182

166183
return null;
167184
}
168-
169-
/**
170-
* Get system log content.
171-
*
172-
* @return string
173-
*/
174-
private function getSystemLogContent(): string
175-
{
176-
$logDir = $this->getLogDirectoryWrite();
177-
$logFile = $logDir->openFile($this->systemLogFileName, 'rb');
178-
$content = $this->tail($logFile, 10);
179-
180-
return $content;
181-
}
182-
183-
/**
184-
* Get file tail.
185-
*
186-
* @param FileWriteInterface $file
187-
* @param int $lines
188-
* @param int $buffer
189-
* @return false|string
190-
*/
191-
private function tail(FileWriteInterface $file, int $lines = 10, int $buffer = 4096)
192-
{
193-
// Jump to last character
194-
$file->seek(-1, SEEK_END);
195-
196-
// Read it and adjust line number if necessary
197-
// (Otherwise the result would be wrong if file doesn't end with a blank line)
198-
if ($file->read(1) != "\n") {
199-
$lines--;
200-
}
201-
202-
// Start reading
203-
$output = '';
204-
$chunk = '';
205-
206-
// While we would like more
207-
while ($file->tell() > 0 && $lines >= 0) {
208-
// Figure out how far back we should jump
209-
$seek = min($file->tell(), $buffer);
210-
211-
// Do the jump (backwards, relative to where we are)
212-
$file->seek(-$seek, SEEK_CUR);
213-
214-
// Read a chunk and prepend it to our output
215-
$output = ($chunk = $file->read($seek)) . $output;
216-
217-
// Jump back to where we started reading
218-
$file->seek(-mb_strlen($chunk, '8bit'), SEEK_CUR);
219-
220-
// Decrease our line counter
221-
$lines -= substr_count($chunk, "\n");
222-
}
223-
224-
// While we have too many lines
225-
// (Because of buffer size we might have read too many)
226-
while ($lines++ < 0) {
227-
// Find first newline and remove all text before that
228-
$output = substr($output, strpos($output, "\n") + 1);
229-
}
230-
231-
// Close file and return
232-
$file->close();
233-
234-
return $output;
235-
}
236-
237-
/**
238-
* Get current LOG directory write.
239-
*
240-
* @return WriteInterface
241-
*/
242-
private function getLogDirectoryWrite()
243-
{
244-
/** @var Filesystem $filesystem */
245-
$filesystem = $this->_objectManager->create(Filesystem::class);
246-
$logDirectory = $filesystem->getDirectoryWrite(DirectoryList::LOG);
247-
248-
return $logDirectory;
249-
}
250185
}

dev/tests/integration/testsuite/Magento/Catalog/_files/attribute_set_based_on_default_without_country_of_manufacture.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use Magento\Catalog\Api\Data\ProductAttributeInterface;
99
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
10-
use Magento\Catalog\Model\Product;
1110
use Magento\Catalog\Model\Product\Attribute\Group;
1211
use Magento\Eav\Model\Entity\Attribute\Set;
1312
use Magento\Eav\Model\Entity\Type;

dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_with_country_of_manufacture.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
99
use Magento\Catalog\Api\ProductRepositoryInterface;
1010
use Magento\TestFramework\Helper\Bootstrap;
11-
use Magento\Catalog\Model\Product;
1211

1312
Bootstrap::getInstance()->reinitialize();
1413

0 commit comments

Comments
 (0)