environment = new Environment($releaseChannels); $this->client = $this->buildClient(); $this->getCache()->flushAll(); } public function test_getPluginInfo_existingPluginOnTheMarketplace() { $plugin = $this->client->getPluginInfo('SecurityInfo'); $expectedPluginKeys = array( 'name', 'displayName', 'owner', 'description', 'homepage', 'createdDateTime', 'donate', 'support', 'isTheme', 'keywords', 'basePrice', 'authors', 'repositoryUrl', 'lastUpdated', 'latestVersion', 'numDownloads', 'screenshots', 'previews', 'activity', 'featured', 'isFree', 'isPaid', 'isCustomPlugin', 'shop', 'versions', 'isDownloadable', 'changelog', 'consumer'); $this->assertNotEmpty($plugin); $this->assertEquals($expectedPluginKeys, array_keys($plugin)); $this->assertSame('SecurityInfo', $plugin['name']); $this->assertSame('piwik', $plugin['owner']); $this->assertTrue(is_array($plugin['keywords'])); $this->assertNotEmpty($plugin['authors']); $this->assertGreaterThan(1000, $plugin['numDownloads']); $this->assertTrue($plugin['isFree']); $this->assertFalse($plugin['isPaid']); $this->assertFalse($plugin['isCustomPlugin']); $this->assertNotEmpty($plugin['versions']); $lastVersion = $plugin['versions'][count($plugin['versions']) - 1]; $this->assertEquals(array('name', 'release', 'requires', 'numDownloads', 'license', 'repositoryChangelogUrl', 'readmeHtml', 'download'), array_keys($lastVersion)); $this->assertNotEmpty($lastVersion['download']); } /** * @expectedException \Piwik\Plugins\Marketplace\Api\Exception * @expectedExceptionMessage Requested plugin does not exist. */ public function test_getPluginInfo_shouldThrowException_IfPluginDoesNotExistOnMarketplace() { $this->client->getPluginInfo('NotExistingPlugIn'); } public function test_getConsumer_shouldReturnNullAndNotThrowException_IfNotAuthorized() { $this->assertNull($this->client->getConsumer()); } public function test_isValidConsumer_shouldReturnFalseAndNotThrowException_IfNotAuthorized() { $this->assertFalse($this->client->isValidConsumer()); } public function test_searchForPlugins_requestAll() { $plugins = $this->client->searchForPlugins($keywords = '', $query = '', $sort = '', $purchaseType = PurchaseType::TYPE_ALL); $this->assertGreaterThan(15, count($plugins)); foreach ($plugins as $plugin) { $this->assertNotEmpty($plugin['name']); $this->assertFalse($plugin['isTheme']); } } public function test_searchForPlugins_onlyFree() { $plugins = $this->client->searchForPlugins($keywords = '', $query = '', $sort = '', $purchaseType = PurchaseType::TYPE_FREE); $this->assertGreaterThan(15, count($plugins)); foreach ($plugins as $plugin) { $this->assertTrue($plugin['isFree']); $this->assertFalse($plugin['isPaid']); $this->assertFalse($plugin['isTheme']); } } public function test_searchForPlugins_onlyPaid() { $plugins = $this->client->searchForPlugins($keywords = '', $query = '', $sort = '', $purchaseType = PurchaseType::TYPE_PAID); $this->assertLessThan(30, count($plugins)); foreach ($plugins as $plugin) { $this->assertFalse($plugin['isFree']); $this->assertTrue($plugin['isPaid']); $this->assertFalse($plugin['isTheme']); } } public function test_searchForPlugins_withKeyword() { $plugins = $this->client->searchForPlugins($keywords = 'login', $query = '', $sort = '', $purchaseType = PurchaseType::TYPE_ALL); $this->assertLessThan(30, count($plugins)); foreach ($plugins as $plugin) { $this->assertContains($keywords, $plugin['keywords']); } } public function test_searchForThemes_requestAll() { $plugins = $this->client->searchForThemes($keywords = '', $query = '', $sort = '', $purchaseType = PurchaseType::TYPE_ALL); $this->assertGreaterThanOrEqual(1, count($plugins)); $this->assertLessThan(50, count($plugins)); foreach ($plugins as $plugin) { $this->assertNotEmpty($plugin['name']); $this->assertTrue($plugin['isTheme']); } } public function test_getDownloadUrl() { $url = $this->client->getDownloadUrl('SecurityInfo'); $start = $this->domain . '/api/2.0/plugins/SecurityInfo/download/'; $end = '?coreVersion=' . Version::VERSION; $this->assertStringStartsWith($start, $url); $this->assertStringEndsWith($end, $url); $version = str_replace(array($start, $end), '', $url); $this->assertNotEmpty($version); $this->assertRegExp('/\d+\.\d+\.\d+/', $version); } public function test_clientResponse_shouldBeCached() { $params = array( 'keywords' => 'login', 'purchase_type' => '', 'query' => '', 'sort' => '', 'release_channel' => 'latest_stable', 'prefer_stable' => 1, 'piwik' => Version::VERSION, 'php' => phpversion(), 'mysql' => $this->environment->getMySQLVersion(), 'num_users' => $this->environment->getNumUsers(), 'num_websites' => $this->environment->getNumWebsites() ); $id = 'marketplace.api.2.0.plugins.' . md5(Http::buildQuery($params)); $cache = $this->getCache(); $this->assertFalse($cache->contains($id)); $this->client->searchForPlugins($keywords = 'login', $query = '', $sort = '', $purchaseType = PurchaseType::TYPE_ALL); $this->assertTrue($cache->contains($id)); $cachedPlugins = $cache->fetch($id); $this->assertInternalType('array', $cachedPlugins); $this->assertNotEmpty($cachedPlugins); $this->assertGreaterThan(30, $cachedPlugins); } public function test_cachedClientResponse_shouldBeReturned() { $params = array( 'keywords' => 'login', 'purchase_type' => '', 'query' => '', 'sort' => '', 'release_channel' => 'latest_stable', 'prefer_stable' => 1, 'piwik' => Version::VERSION, 'php' => phpversion(), 'mysql' => $this->environment->getMySQLVersion(), 'num_users' => $this->environment->getNumUsers(), 'num_websites' => $this->environment->getNumWebsites()); $id = 'marketplace.api.2.0.plugins.' . md5(Http::buildQuery($params)); $cache = $this->getCache(); $cache->save($id, array('plugins' => array(array('name' => 'foobar')))); $result = $this->client->searchForPlugins($keywords = 'login', $query = '', $sort = '', $purchaseType = PurchaseType::TYPE_ALL); $this->assertSame(array(array('name' => 'foobar')), $result); } public function test_getInfoOfPluginsHavingUpdate() { $service = new TestService($this->domain); $client = $this->buildClient($service); $pluginTest = array(); if (!Plugin\Manager::getInstance()->isPluginLoaded('CustomAlerts')) { $pluginTest[] = Plugin\Manager::getInstance()->loadPlugin('CustomAlerts'); } else { $pluginTest[] = Plugin\Manager::getInstance()->getLoadedPlugin('CustomAlerts'); } $client->getInfoOfPluginsHavingUpdate($pluginTest); $this->assertSame('plugins/checkUpdates', $service->action); $this->assertSame(array('plugins', 'release_channel', 'prefer_stable', 'piwik', 'php', 'mysql', 'num_users', 'num_websites'), array_keys($service->params)); $plugins = $service->params['plugins']; $this->assertInternalType('string', $plugins); $this->assertJson($plugins); $plugins = json_decode($plugins, true); $names = array( 'CustomAlerts' => true, ); foreach ($plugins['plugins'] as $plugin) { $this->assertNotEmpty($plugin['version']); unset($names[$plugin['name']]); } $this->assertEmpty($names); } private function buildClient($service = null) { if (!isset($service)) { $service = new Service($this->domain); } return new Client($service, $this->getCache(), new NullLogger(), $this->environment); } private function getCache() { return Cache::getLazyCache(); } }