Skip to content

Commit

Permalink
Add tests for MarketplaceSourceProvider class
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Sep 4, 2024
1 parent 056d93f commit efaf05e
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 3 deletions.
2 changes: 1 addition & 1 deletion classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ public function getModuleSourceProviders(): array

$this->moduleSourceProviders = [
new LocalSourceProvider($this->getProperty(self::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . 'modules', $this->getFileConfigurationStorage()),
new MarketplaceSourceProvider($this->getState()->getInstallVersion(), $this->getFileLoader(), $this->getFileConfigurationStorage()),
new MarketplaceSourceProvider($this->getState()->getInstallVersion(), $this->getProperty(self::PS_ROOT_PATH), $this->getFileLoader(), $this->getFileConfigurationStorage()),
// Other providers
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ class MarketplaceSourceProvider implements ModuleSourceProviderInterface
/** @var string */
private $targetVersionOfPrestaShop;

public function __construct(string $targetVersionOfPrestaShop, FileLoader $fileLoader, FileConfigurationStorage $fileConfigurationStorage)
/** @var string */
private $prestashopRootFolder;

public function __construct(string $targetVersionOfPrestaShop, string $prestashopRootFolder, FileLoader $fileLoader, FileConfigurationStorage $fileConfigurationStorage)
{
$this->targetVersionOfPrestaShop = $targetVersionOfPrestaShop;
$this->prestashopRootFolder = $prestashopRootFolder;
$this->fileLoader = $fileLoader;
$this->fileConfigurationStorage = $fileConfigurationStorage;
}
Expand Down Expand Up @@ -98,10 +102,16 @@ public function warmUp(): void
]);

$xml = $this->fileLoader->getXmlFile(
_PS_ROOT_DIR_ . '/config/xml/modules_native_addons.xml',
$this->prestashopRootFolder . '/config/xml/modules_native_addons.xml',
self::ADDONS_API_URL . '/?' . $postData
);

if ($xml === false) {
return;
}

$this->localModuleZips = [];

foreach ($xml as $moduleInXml) {
$this->localModuleZips[] = new ModuleSource(
(string) $moduleInXml->name,
Expand Down
122 changes: 122 additions & 0 deletions tests/fixtures/api-marketplace/native-modules-list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="UTF-8"?>
<modules><module>
<id>5496</id>
<name>autoupgrade</name>
<displayName>1-Click Upgrade</displayName>
<categoryName><![CDATA[Data migration & Backup]]></categoryName>
<description>
Upgrade to the latest version of PrestaShop in a few clicks, thanks to this automated method
</description>
<author>PrestaShop</author>
<img>https://medias1.prestastore.com/img/pico/5496.jpg</img>
<description_full>
Upgrade to the latest version of PrestaShop in a few clicks, thanks to this automated method.
</description_full>
<additional_description>
Use this native module to:Keep your store up-to-date: benefit from the latest features and improvements available.Protect your store: stay up-to-date with the latest on security for modules you have downloaded from PrestaShop Addons.
</additional_description>
<tab>migration_tools</tab>
<version>6.0.0</version>
<compatibility>
<from>1.7.0.0</from>
<to>8.1.7</to>
</compatibility>
<price>
<EUR>0</EUR>
<USD>0</USD>
<GBP>0</GBP>
</price>
<badges>
<badge label="Made by PrestaShop">
https://medias2.prestastore.com/themes/prestastore/img/front/sprites/badges/made-by-prestashop.png
</badge>
</badges>
<cover>
<small>
https://addons.prestashop.com/1630684-pprod/1-click-upgrade.jpg
</small>
<big>
https://addons.prestashop.com/1630684-pbig/1-click-upgrade.jpg
</big>
</cover>
</module>
<module>
<id>9131</id>
<name>blockwishlist</name>
<displayName>Wishlist block</displayName>
<categoryName>Non téléchargeables</categoryName>
<description>
Allow customers to create wishlists to save their favorite products for later
</description>
<author>PrestaShop</author>
<img>https://medias1.prestastore.com/img/pico/9131.jpg</img>
<description_full>
Allow customers to create wishlists to save their favorite products for later.
</description_full>
<tab></tab>
<version>3.0.1</version>
<compatibility>
<from>8.0.0</from>
<to>8.1.7</to>
</compatibility>
<price>
<EUR>0</EUR>
<USD>0</USD>
<GBP>0</GBP>
</price>
<badges>
<badge label="Made by PrestaShop">
https://medias2.prestastore.com/themes/prestastore/img/front/sprites/badges/made-by-prestashop.png
</badge>
</badges>
<cover>
<small>
https://addons.prestashop.com/1483984-pprod/wishlist-block.jpg
</small>
<big>
https://addons.prestashop.com/1483984-pbig/wishlist-block.jpg
</big>
</cover>
</module>
<module>
<id>9144</id>
<name>productcomments</name>
<displayName>Product Comments</displayName>
<categoryName>Non téléchargeables</categoryName>
<description>
Allow users to post reviews on your products and/or rate them based on specific criteria
</description>
<author>PrestaShop</author>
<img>https://medias1.prestastore.com/img/pico/9144.jpg</img>
<description_full>
Allow users to post reviews on your products and/or rate them based on specific criteria.
</description_full>
<additional_description>
Use this native module to:Boost your sales: increase your conversion rate with a comments section, helpful to reassure and show your visitors your store is trustworthy.Improve your SEO: search engines will use it to discover new content.Rich snippets: enrich your search engine results by displaying rating stars on your store - it allows you to gain visibility, and therefore increase your traffic.
</additional_description>
<tab></tab>
<version>6.0.2</version>
<compatibility>
<from>1.7.7.0</from>
<to>8.1.7</to>
</compatibility>
<price>
<EUR>0</EUR>
<USD>0</USD>
<GBP>0</GBP>
</price>
<badges>
<badge label="Made by PrestaShop">
https://medias2.prestastore.com/themes/prestastore/img/front/sprites/badges/made-by-prestashop.png
</badge>
</badges>
<cover>
<small>
https://addons.prestashop.com/1430579-pprod/product-comments.jpg
</small>
<big>
https://addons.prestashop.com/1430579-pbig/product-comments.jpg
</big>
</cover>
</module>
</modules>
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\Parameters\FileConfigurationStorage;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\ModuleSource;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Module\Source\Provider\MarketplaceSourceProvider;
use PrestaShop\Module\AutoUpgrade\Xml\FileLoader;

class MarketplaceSourceProviderTest extends TestCase
{
protected function setUp()
{
parent::setUp();

if (PHP_VERSION_ID >= 80000) {
$this->markTestSkipped('An issue with this version of PHPUnit and PHP 8+ prevents this test to run.');
}
}

public function testCacheGenerationWithData()
{
$fileLoader = $this->createMock(FileLoader::class);
$fileLoader->method('getXmlFile')->willReturn(simplexml_load_file(__DIR__ . '/../../../../../fixtures/api-marketplace/native-modules-list.xml'));
$fileConfigurationStorageMock = $this->createMock(FileConfigurationStorage::class);

$sourceProvider = new MarketplaceSourceProvider('9.0.0', 'C:\mocked', $fileLoader, $fileConfigurationStorageMock);

$fileConfigurationStorageMock->expects($this->once())->method('exists');
$fileConfigurationStorageMock->expects($this->once())->method('save');
$fileConfigurationStorageMock->expects($this->never())->method('load');

$results1 = $sourceProvider->getUpdatesOfModule('blockwishlist', '0.9.0');
$results2 = $sourceProvider->getUpdatesOfModule('blockwishlist', '0.9.0');

$this->assertEquals($results1, $results2);
$this->assertEquals([
new ModuleSource('blockwishlist', '3.0.1', 'https://api.addons.prestashop.com/?id_module=9131&method=module&version=9.0.0', true),
], $results2);
}

public function testCacheGenerationWithNoData()
{
$fileLoader = $this->createMock(FileLoader::class);
$fileLoader->method('getXmlFile')->willReturn(simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><modules></modules>'));
$fileConfigurationStorageMock = $this->createMock(FileConfigurationStorage::class);

$sourceProvider = new MarketplaceSourceProvider('9.0.0', 'C:\mocked', $fileLoader, $fileConfigurationStorageMock);

$fileConfigurationStorageMock->expects($this->once())->method('exists');
$fileConfigurationStorageMock->expects($this->once())->method('save');
$fileConfigurationStorageMock->expects($this->never())->method('load');

$sourceProvider->getUpdatesOfModule('test1', '1.0.0');
$sourceProvider->getUpdatesOfModule('test2', '1.0.0');
}

public function testCacheLoading()
{
$fileLoader = $this->createMock(FileLoader::class);
$fileConfigurationStorageMock = $this->createMock(FileConfigurationStorage::class);
$fileConfigurationStorageMock->method('exists')->willReturn(true);
$fileConfigurationStorageMock->method('load')->willReturn([]);

$sourceProvider = new MarketplaceSourceProvider('9.0.0', 'C:\mocked', $fileLoader, $fileConfigurationStorageMock);

$fileConfigurationStorageMock->expects($this->once())->method('exists');
$fileConfigurationStorageMock->expects($this->once())->method('load');
$fileConfigurationStorageMock->expects($this->never())->method('save');

$sourceProvider->getUpdatesOfModule('test1', '1.0.0');
$sourceProvider->getUpdatesOfModule('test2', '1.0.0');
}
}

0 comments on commit efaf05e

Please sign in to comment.