This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,80 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Review\Block\Account;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Checks "My Product Reviews" link displaying in customer account dashboard
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
class LinkTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Page */
private $page;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->page = $this->objectManager->get(PageFactory::class)->create();
}
/**
* @return void
*/
public function testMyProductReviewsLink(): void
{
$this->preparePage();
$block = $this->page->getLayout()->getBlock('customer-account-navigation-product-reviews-link');
$this->assertNotFalse($block);
$html = $block->toHtml();
$this->assertStringContainsString('/review/customer/', $html);
$this->assertEquals((string)__('My Product Reviews'), strip_tags($html));
}
/**
* @magentoConfigFixture current_store catalog/review/active 0
*
* @return void
*/
public function testMyProductReviewsLinkDisabled(): void
{
$this->preparePage();
$block = $this->page->getLayout()->getBlock('customer-account-navigation-product-reviews-link');
$this->assertFalse($block);
}
/**
* Prepare page before render
*
* @return void
*/
private function preparePage(): void
{
$this->page->addHandle([
'default',
'customer_account',
]);
$this->page->getLayout()->generateXml();
}
}
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Review\Block\Adminhtml\Edit;
use Magento\Customer\Model\Customer;
use Magento\Framework\Escaper;
use Magento\Framework\Registry;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
class FormTest extends TestCase
{
/**
* @magentoDataFixture Magento/Review/_files/customer_review.php
*/
public function testCustomerOnForm()
{
/** @var Customer $customer */
$customer = Bootstrap::getObjectManager()->create(Customer::class)
->setWebsiteId(1)
->loadByEmail('customer@example.com');
$block = Bootstrap::getObjectManager()->create(Form::class);
$block->setNameInLayout('test_block_name');
/** @var Escaper $escaper */
$escaper = Bootstrap::getObjectManager()->get(Escaper::class);
$this->assertStringMatchesFormat(
'%A' . __(
'<a href="%1" onclick="this.target=\'blank\'">%2 %3</a> <a href="mailto:%4">(%4)</a>',
'%A',
$escaper->escapeHtml($customer->getFirstname()),
$escaper->escapeHtml($customer->getLastname()),
$escaper->escapeHtml($customer->getEmail())
) . '%A',
$block->toHtml()
);
}
/**
* Verify review form hidden input will contain all review stores.
*
* @magentoDataFixture Magento/Review/_files/customer_review.php
* @return void
*/
public function testStoresOnForm(): void
{
$registry = Bootstrap::getObjectManager()->get(Registry::class);
$review = $registry->registry('review_data');
$block = Bootstrap::getObjectManager()->create(Form::class);
$block->setNameInLayout('test_block_name');
foreach ($review->getStores() as $storeId) {
$regex = sprintf('/input id="select_stores" (.*) value="%d" type="hidden"/', $storeId);
$this->assertMatchesRegularExpression(
$regex,
$block->toHtml()
);
}
}
}
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Review\Block\Adminhtml\Edit\Tab;
/**
* @magentoAppArea adminhtml
*/
class FormTest extends \PHPUnit\Framework\TestCase
{
public function testConstruct()
{
$this->assertInstanceOf(
\Magento\Review\Block\Adminhtml\Rating\Edit\Tab\Form::class,
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Framework\View\LayoutInterface::class
)->createBlock(
\Magento\Review\Block\Adminhtml\Rating\Edit\Tab\Form::class
)
);
}
}
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Review\Block\Adminhtml\Edit\Tab;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Checks customer products reviews tab
*
* @see \Magento\Review\Block\Adminhtml\Edit\Tab\Reviews
* @magentoAppArea adminhtml
*/
class ReviewsTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Reviews */
private $block;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Reviews::class);
}
/**
* @magentoDataFixture Magento/Review/_files/customer_review.php
*
* @return void
*/
public function testReviewGrid(): void
{
$this->block->setCustomerId(1);
$this->assertCount(1, $this->block->getPreparedCollection());
}
}
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Review\Block\Adminhtml;
class MainTest extends \PHPUnit\Framework\TestCase
{
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoAppArea adminhtml
*/
public function testConstruct()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Customer\Api\AccountManagementInterface $accountManagement */
$accountManagement = $objectManager->create(\Magento\Customer\Api\AccountManagementInterface::class);
/** @var \Magento\Customer\Helper\View $customerViewHelper */
$customerViewHelper = $objectManager->create(\Magento\Customer\Helper\View::class);
$customer = $accountManagement->authenticate('customer@example.com', 'password');
$request = $objectManager->get(\Magento\Framework\App\RequestInterface::class);
$request->setParam('customerId', $customer->getId());
/** @var \Magento\Framework\View\LayoutInterface $layout */
$layout = $objectManager->get(\Magento\Framework\View\LayoutInterface::class);
$block = $layout->createBlock(\Magento\Review\Block\Adminhtml\Main::class);
$customerName = $customerViewHelper->getCustomerName($customer);
/** @var \Magento\Framework\Escaper $escaper */
$escaper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Framework\Escaper::class);
$this->assertStringMatchesFormat(
'%A' . __('All Reviews of Customer `%1`', $escaper->escapeHtml($customerName)) . '%A',
$block->getHeaderHtml()
);
}
}
@@ -0,0 +1,135 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Review\Block\Customer;
use Magento\Customer\Model\Session;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Test for customer product reviews grid.
*
* @see \Magento\Review\Block\Customer\ListCustomer
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class ListCustomerTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Session */
private $customerSession;
/** @var ListCustomer */
private $block;
/** @var CollectionFactory */
private $collectionFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->customerSession = $this->objectManager->get(Session::class);
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(ListCustomer::class)
->setTemplate('Magento_Review::customer/list.phtml');
$this->collectionFactory = $this->objectManager->get(CollectionFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Review/_files/customer_review_with_rating.php
*
* @return void
*/
public function testCustomerProductReviewsGrid(): void
{
$this->customerSession->setCustomerId(1);
$review = $this->collectionFactory->create()->addCustomerFilter(1)->addReviewSummary()->getFirstItem();
$this->assertNotNull($review->getReviewId());
$blockHtml = $this->block->toHtml();
$createdDate = $this->block->dateFormat($review->getReviewCreatedAt());
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf("//td[contains(@class, 'date') and contains(text(), '%s')]", $createdDate),
$blockHtml
),
sprintf('Created date wasn\'t found or not equals to %s.', $createdDate)
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf("//td[contains(@class, 'item')]//a[contains(text(), '%s')]", $review->getName()),
$blockHtml
),
'Product name wasn\'t found.'
);
$rating = $review->getSum() / $review->getCount();
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf("//td[contains(@class, 'summary')]//span[contains(text(), '%s%%')]", $rating),
$blockHtml
),
sprintf('Rating wasn\'t found or not equals to %s%%.', $rating)
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf("//td[contains(@class, 'description') and contains(text(), '%s')]", $review->getDetail()),
$blockHtml
),
'Review description wasn\'t found.'
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//td[contains(@class, 'actions')]//a[contains(@href, '%s')]/span[contains(text(), '%s')]",
$this->block->getReviewUrl($review),
__('See Details')
),
$blockHtml
),
sprintf('%s button wasn\'t found.', __('See Details'))
);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @return void
*/
public function testCustomerWithoutReviews(): void
{
$this->customerSession->setCustomerId(1);
$this->assertStringContainsString(
(string)__('You have submitted no reviews.'),
strip_tags($this->block->toHtml())
);
}
}
@@ -0,0 +1,137 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Review\Block\Customer;
use Magento\Customer\Model\Session;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Test for displaying customer product review block.
*
* @see \Magento\Review\Block\Customer\View
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class ViewTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Session */
private $customerSession;
/** @var CollectionFactory */
private $collectionFactory;
/** @var View */
private $block;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->customerSession = $this->objectManager->get(Session::class);
$this->collectionFactory = $this->objectManager->get(CollectionFactory::class);
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(View::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Review/_files/customer_review_with_rating.php
*
* @return void
*/
public function testCustomerProductReviewBlock(): void
{
$this->customerSession->setCustomerId(1);
$review = $this->collectionFactory->create()->addCustomerFilter(1)->getFirstItem();
$this->assertNotNull($review->getReviewId());
$blockHtml = $this->block->setReviewId($review->getReviewId())->toHtml();
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf("//div[contains(@class, 'product-info')]/h2[contains(text(), '%s')]", $review->getName()),
$blockHtml
),
'Product name wasn\'t found.'
);
$ratings = $this->block->getRating();
$this->assertCount(2, $ratings);
foreach ($ratings as $rating) {
$this->assertNotEmpty($rating->getRatingId());
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@id, 'rating-div-".$rating->getRatingId()."')]//span[contains(text(), '%s')]",
$rating->getPercent()
),
$blockHtml
),
sprintf('Rating %s was not found or not equals to %s.', $rating->getRatingCode(), $rating->getPercent())
);
}
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf("//div[contains(@class, 'review-title') and contains(text(), '%s')]", $review->getTitle()),
$blockHtml
),
'Review title wasn\'t found.'
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf("//div[contains(@class, 'review-content') and contains(text(), '%s')]", $review->getDetail()),
$blockHtml
),
'Review description wasn\'t found.'
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'review-date') and contains(text(), '%s')]/time[contains(text(), '%s')]",
__('Submitted on'),
$this->block->dateFormat($review->getCreatedAt())
),
$blockHtml
),
'Created date wasn\'t found.'
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//a[contains(@href, '/review/customer/')]/span[contains(text(), '%s')]",
__('Back to My Reviews')
),
$blockHtml
),
sprintf('%s button wasn\'t found.', __('Back to My Reviews'))
);
}
}
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Review\Block;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\Value;
use Magento\Framework\App\ReinitableConfig;
use Magento\Framework\App\State;
use Magento\Framework\View\Element\ButtonLockManager;
use Magento\TestFramework\ObjectManager;
class FormTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManager;
*/
private $objectManager;
protected function setUp(): void
{
$this->objectManager = $this->getObjectManager();
parent::setUp();
}
/**
* @magentoDbIsolation enabled
* @magentoDataFixture Magento/Review/_files/config.php
* @dataProvider getCorrectFlagDataProvider
*/
public function testGetCorrectFlag(
$path,
$scope,
$scopeId,
$value,
$expectedResult
) {
/** @var State $appState */
$appState = $this->objectManager->get(State::class);
$appState->setAreaCode(Area::AREA_FRONTEND);
/** @var Value $config */
$config = $this->objectManager->create(Value::class);
$config->setPath($path);
$config->setScope($scope);
$config->setScopeId($scopeId);
$config->setValue($value);
$config->save();
/** @var ReinitableConfig $reinitableConfig */
$reinitableConfig = $this->objectManager->create(ReinitableConfig::class);
$reinitableConfig->reinit();
/** @var \Magento\Review\Block\Form $form */
$form = $this->objectManager->create(\Magento\Review\Block\Form::class);
$form->setButtonLockManager(
$this->objectManager->create(ButtonLockManager::class, ['buttonLockPool' => []])
);
$result = $form->getAllowWriteReviewFlag();
$this->assertEquals($result, $expectedResult);
}
public function getCorrectFlagDataProvider()
{
return [
[
'path' => 'catalog/review/allow_guest',
'scope' => 'websites',
'scopeId' => '1',
'value' => 0,
'expectedResult' => false,
],
[
'path' => 'catalog/review/allow_guest',
'scope' => 'websites',
'scopeId' => '1',
'value' => 1,
'expectedResult' => true
]
];
}
private function getObjectManager()
{
return \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
}
}
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Review\Block\Product;
/**
* Test for \Magento\Review\Block\Product\ReviewRenderer
*/
class ReviewRendererTest extends \PHPUnit\Framework\TestCase
{
/**
* Test verifies ReviewRenderer::getReviewsSummaryHtml call with $displayIfNoReviews = false
* The reviews summary will be shown as expected only if there is at least one review available
*
* @magentoDataFixture Magento/Review/_files/different_reviews.php
* @magentoAppArea frontend
*/
public function testGetReviewSummaryHtml()
{
$productSku = 'simple';
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$product = $productRepository->get($productSku);
/** @var ReviewRenderer $reviewRenderer */
$reviewRenderer = $objectManager->create(ReviewRenderer::class);
$actualResult = $reviewRenderer->getReviewsSummaryHtml($product);
$this->assertEquals(2, $reviewRenderer->getReviewsCount());
$this->assertStringContainsString('<span itemprop="reviewCount">2</span>', $actualResult);
}
}
@@ -0,0 +1,143 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Review\Block;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Customer\Model\Session;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutInterface;
use Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Test for displaying product review block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class ViewTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Session */
private $customerSession;
/** @var CollectionFactory */
private $collectionFactory;
/** @var Registry */
private $registry;
/** @var View */
private $block;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->customerSession = $this->objectManager->get(Session::class);
$this->collectionFactory = $this->objectManager->get(CollectionFactory::class);
$this->registry = $this->objectManager->get(Registry::class);
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(View::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_review');
$this->registry->unregister('current_product');
$this->registry->unregister('product');
$this->customerSession->setCustomerId(null);
parent::tearDown();
}
/**
* Test product review block
*
* @magentoDataFixture Magento/Review/_files/product_review_with_rating.php
*
* @return void
* @throws NoSuchEntityException
*/
public function testProductReviewBlock(): void
{
$this->customerSession->setCustomerId(1);
$review = $this->collectionFactory->create()->addCustomerFilter(1)->getFirstItem();
$this->registerReview($review);
$this->assertNotNull($review->getReviewId());
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
/** @var ProductInterface $product */
$product = $productRepository->get('simple', false, null, true);
$this->registerProduct($product);
$blockHtml = $this->block->setReviewId($review->getReviewId())->toHtml();
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf("//div[contains(@class, 'details')]/h3[contains(text(), '%s')]", $review->getName()),
$blockHtml
),
'Product name wasn\'t found.'
);
$ratings = $this->block->getRating();
$this->assertCount(2, $ratings);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//a[contains(@class, 'action back')]/span[contains(text(), '%s')]",
__('Back to Product Reviews')
),
$blockHtml
),
sprintf('%s button wasn\'t found.', __('Back to Product Reviews'))
);
}
/**
* Register the product
*
* @param ProductInterface $product
* @return void
*/
private function registerProduct(ProductInterface $product): void
{
$this->registry->unregister('current_product');
$this->registry->unregister('product');
$this->registry->register('current_product', $product);
$this->registry->register('product', $product);
}
/**
* Register the current review
*
* @param Product $review
* @return void
*/
private function registerReview(Product $review): void
{
$this->registry->unregister('current_review');
$this->registry->register('current_review', $review);
}
}