1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-22 15:07:44 +00:00
Files
matomo/plugins/Marketplace/tests/System/Api/ServiceTest.php
Stefan Giehl d6d72d1fa7 [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps (#22144)
* [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps

* [Coding Style] Use camel case for method names in API plugin tests (#22145)

* [Coding Style] Use camel case for method names in Core* plugin tests (#22147)

* [Coding Style] Use camel case for method names in core Unit tests (#22149)

* [Coding Style] Use camel case for method names in Actions and BulkTracking plugin tests (#22146)

* [Coding Style] Use camel case for method names in CustomDimensions and CustomJSTracker plugin tests (#22148)

* [Coding Style] Use camel case for method names in core Integration tests (#22151)

* [Coding Style] Use camel case for method names in more core plugin tests (#22153)

* [Coding Style] Use camel case for method names in more core plugin tests (#22157)

* [Coding Style] Use camel case for method names in more core plugin tests

* Update plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php

Co-authored-by: Michal Kleiner <michal@innocraft.com>

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>

* [Coding Style] Use camel case for method names in more core plugin tests (#22159)

* [Coding Style] Use camel case for method names in remaining tests (#22160)

* [Coding Style] Use camel case for method names in remaining tests

* rename expected test files

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>
2024-04-25 20:57:55 +02:00

156 خطوط
5.1 KiB
PHP

<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Marketplace\tests\System\Api;
use Piwik\Container\StaticContainer;
use Piwik\Filesystem;
use Piwik\Plugins\Marketplace\Api\Service;
use Piwik\Tests\Framework\TestCase\SystemTestCase;
/**
* @group Plugins
* @group Marketplace
* @group ServiceTest
* @group Service
*/
class ServiceTest extends SystemTestCase
{
private $domain = 'http://plugins.piwik.org';
public function testShouldUseVersion2()
{
$service = $this->buildService();
$this->assertSame('2.0', $service->getVersion());
}
public function testGetDomainShouldReturnPassedDomain()
{
$service = $this->buildService();
$this->assertSame($this->domain, $service->getDomain());
}
public function testAuthenticateGetAccessTokenShouldSaveTokenIfOnlyHasAlNumValues()
{
$service = $this->buildService();
$service->authenticate('123456789abcdefghij');
$this->assertSame('123456789abcdefghij', $service->getAccessToken());
}
public function testHasAccessToken()
{
$service = $this->buildService();
$this->assertFalse($service->hasAccessToken());
$service->authenticate('123456789abcdefghij');
$this->assertTrue($service->hasAccessToken());
}
public function testAuthenticateGetAccessTokenEmptyTokenShouldUnsetToken()
{
$service = $this->buildService();
$service->authenticate('');
$this->assertNull($service->getAccessToken());
}
public function testAuthenticateGetAccessTokenInvalidTokenContainingInvalidCharactersShouldBeIgnored()
{
$service = $this->buildService();
$service->authenticate('123_-4?');
$this->assertNull($service->getAccessToken());
}
public function testFetchShouldCallMarketplaceApiWithActionAndReturnArrays()
{
$service = $this->buildService();
$response = $service->fetch('plugins', array());
$this->assertTrue(is_array($response));
$this->assertArrayHasKey('plugins', $response);
$this->assertGreaterThanOrEqual(30, count($response['plugins']));
foreach ($response['plugins'] as $plugin) {
$this->assertArrayHasKey('name', $plugin);
}
}
public function testFetchShouldCallMarketplaceApiWithGivenParamsAndReturnArrays()
{
$keyword = 'login';
$service = $this->buildService();
$response = $service->fetch('plugins', array('keywords' => $keyword));
$this->assertLessThan(20, count($response['plugins']));
foreach ($response['plugins'] as $plugin) {
self::assertTrue(in_array($keyword, $plugin['keywords']));
}
}
public function testFetchShouldThrowExceptionWhenNotBeingAuthenticated()
{
$this->expectException(\Piwik\Plugins\Marketplace\Api\Service\Exception::class);
$this->expectExceptionCode(101);
$this->expectExceptionMessage('Not authenticated');
$service = $this->buildService();
$service->fetch('consumer', array());
}
public function testFetchShouldThrowExceptionWhenBeingAuthenticatedWithInvalidTokens()
{
$this->expectException(\Piwik\Plugins\Marketplace\Api\Service\Exception::class);
$this->expectExceptionCode(101);
$this->expectExceptionMessage('Not authenticated');
$service = $this->buildService();
$service->authenticate('1234567890');
$service->fetch('consumer', array());
}
public function testDownloadShouldReturnRawResultForAbsoluteUrl()
{
$service = $this->buildService();
$response = $service->download($this->domain . '/api/2.0/plugins');
self::assertIsString($response);
$this->assertNotEmpty($response);
$this->assertStringStartsWith('{"plugins"', $response);
}
public function testDownloadShouldSaveResultInFileIfPathGiven()
{
$path = StaticContainer::get('path.tmp') . '/marketplace_test_file.json';
Filesystem::deleteFileIfExists($path);
$service = $this->buildService();
$response = $service->download($this->domain . '/api/2.0/plugins', $path);
$this->assertTrue($response);
$this->assertFileExists($path);
$content = file_get_contents($path);
$this->assertNotEmpty($content);
$this->assertStringStartsWith('{"plugins"', $content);
Filesystem::deleteFileIfExists($path);
}
public function testTimeoutInvalidServiceShouldFailIfNotReachable()
{
// The exact exception may vary depending on the connection backend being used (curl, sockets, fopen, etc), so
// we just check that some exception is thrown when the method is passed an invalid domain
$this->expectException(\Exception::class);
$service = $this->buildService();
$service->download('http://notexisting49.plugins.piwk.org/api/2.0/plugins', null);
}
private function buildService()
{
return new Service($this->domain);
}
}