public function testSaveAuthJson()
 {
     $directoryWrite = $this->getMockForAbstractClass('\\Magento\\Framework\\Filesystem\\Directory\\WriteInterface');
     $this->filesystem->expects($this->once())->method('getDirectoryWrite')->will($this->returnValue($directoryWrite));
     $directoryWrite->expects($this->once())->method('writeFile')->willReturn(true);
     $this->assertTrue($this->packagesAuth->saveAuthJson("testusername", "testpassword"));
 }
 public function testRemoveCredentialsWithError()
 {
     $this->packagesAuth->expects($this->once())->method('removeCredentials')->will($this->throwException(new \Exception()));
     $jsonModel = $this->controller->removeCredentialsAction();
     $this->assertInstanceOf('\\Zend\\View\\Model\\JsonModel', $jsonModel);
     $variables = $jsonModel->getVariables();
     $this->assertArrayHasKey('success', $variables);
     $this->assertArrayHasKey('message', $variables);
     $this->assertFalse($variables['success']);
 }
Example #3
0
 /**
  * Retrieve all available versions for a package
  *
  * @param string $package
  * @return array
  * @throws \RuntimeException
  */
 private function getPackageAvailableVersions($package)
 {
     $magentoRepositories = $this->composerInformation->getRootRepositories();
     // Check we have only one repo.magento.com repository
     if (count($magentoRepositories) === 1 && strpos($magentoRepositories[0], $this->packagesAuth->getCredentialBaseUrl())) {
         $packagesJsonData = $this->getPackagesJson();
         if ($packagesJsonData) {
             $packagesJsonData = json_decode($packagesJsonData, true);
         } else {
             $packagesJsonData['packages'] = [];
         }
         if (isset($packagesJsonData['packages'][$package])) {
             $packageVersions = $packagesJsonData['packages'][$package];
             uksort($packageVersions, 'version_compare');
             $packageVersions = array_reverse($packageVersions);
             return array_keys($packageVersions);
         }
     } else {
         $versionsPattern = '/^versions\\s*\\:\\s(.+)$/m';
         $commandParams = [self::PARAM_COMMAND => self::COMPOSER_SHOW, self::PARAM_PACKAGE => $package, self::PARAM_AVAILABLE => true];
         $applicationFactory = $this->objectManagerProvider->get()->get('Magento\\Framework\\Composer\\MagentoComposerApplicationFactory');
         /** @var \Magento\Composer\MagentoComposerApplication $application */
         $application = $applicationFactory->create();
         $result = $application->runComposerCommand($commandParams);
         $matches = [];
         preg_match($versionsPattern, $result, $matches);
         if (isset($matches[1])) {
             return explode(', ', $matches[1]);
         }
     }
     throw new \RuntimeException(sprintf('Couldn\'t get available versions for package %s', $package));
 }
 public function testComponentsAction()
 {
     $objectManager = $this->getMock('Magento\\Framework\\ObjectManagerInterface', [], [], '', false);
     $this->objectManagerProvider->expects($this->once())->method('get')->willReturn($objectManager);
     $objectManager->expects($this->any())->method('get')->willReturnMap([['Magento\\Framework\\Module\\PackageInfoFactory', $this->packageInfoFactoryMock], ['Magento\\Framework\\Module\\FullModuleList', $this->fullModuleListMock], ['Magento\\Framework\\Module\\ModuleList', $this->enabledModuleListMock]]);
     $this->packageInfoFactoryMock->expects($this->once())->method('create')->willReturn($this->packageInfo);
     $this->fullModuleListMock->expects($this->once())->method('getNames')->willReturn(['magento/sample-module1']);
     $this->packageInfo->expects($this->once())->method('getModuleName')->willReturn('Sample_Module');
     $this->packageInfo->expects($this->exactly(2))->method('getPackageName')->willReturn($this->componentData['magento/sample-module-one']['name']);
     $this->packageInfo->expects($this->exactly(2))->method('getVersion')->willReturn($this->componentData['magento/sample-module-one']['version']);
     $this->enabledModuleListMock->expects($this->once())->method('has')->willReturn(true);
     $this->composerInformationMock->expects($this->once())->method('getInstalledMagentoPackages')->willReturn($this->componentData);
     $this->composerInformationMock->expects($this->once())->method('isPackageInComposerJson')->willReturn(true);
     $this->packagesAuth->expects($this->once())->method('getAuthJsonData')->willReturn(['username' => 'someusername', 'password' => 'somepassword']);
     $this->packagesData->expects($this->once())->method('syncPackagesData')->willReturn($this->lastSyncData);
     $jsonModel = $this->controller->componentsAction();
     $this->assertInstanceOf('Zend\\View\\Model\\JsonModel', $jsonModel);
     $variables = $jsonModel->getVariables();
     $this->assertArrayHasKey('success', $variables);
     $this->assertTrue($variables['success']);
     $expected = [['name' => 'magento/sample-module-one', 'type' => 'magento2-module', 'version' => '1.0.0', 'update' => false, 'uninstall' => true, 'vendor' => 'magento', 'moduleName' => 'Sample_Module', 'enable' => true, 'disable' => false]];
     $this->assertEquals($expected, $variables['components']);
     $this->assertArrayHasKey('total', $variables);
     $this->assertEquals(1, $variables['total']);
     $this->assertEquals($this->lastSyncData, $variables['lastSyncData']);
 }
Example #5
0
 /**
  * Remove credentials from auth.json
  *
  * @return JsonModel
  */
 public function removeCredentialsAction()
 {
     try {
         $result = $this->packagesAuth->removeCredentials();
         return new JsonModel(['success' => $result]);
     } catch (\Exception $e) {
         return new JsonModel(['success' => false, 'message' => $e->getMessage()]);
     }
 }
Example #6
0
 /**
  * Get Components info action
  *
  * @return \Zend\View\Model\JsonModel
  * @throws \RuntimeException
  */
 public function componentsAction()
 {
     $objectManager = $this->objectManagerProvider->get();
     $enabledModuleList = $objectManager->get('Magento\\Framework\\Module\\ModuleList');
     $this->fullModuleList = $objectManager->get('Magento\\Framework\\Module\\FullModuleList');
     $this->packageInfo = $objectManager->get('Magento\\Framework\\Module\\PackageInfoFactory')->create();
     $lastSyncData = [];
     $authDetails = $this->packagesAuth->getAuthJsonData();
     if ($authDetails) {
         $lastSyncData = $this->packagesData->syncPackagesData();
     }
     $components = $this->composerInformation->getInstalledMagentoPackages();
     $allModules = $this->getAllModules();
     $components = array_replace_recursive($components, $allModules);
     foreach ($components as $component) {
         $components[$component['name']]['update'] = false;
         $components[$component['name']]['uninstall'] = false;
         $components[$component['name']]['moduleName'] = $this->packageInfo->getModuleName($component['name']);
         if ($this->composerInformation->isPackageInComposerJson($component['name'])) {
             if ($component['type'] !== \Magento\Framework\Composer\ComposerInformation::METAPACKAGE_PACKAGE_TYPE) {
                 $components[$component['name']]['uninstall'] = true;
             }
             if (isset($lastSyncData['packages'][$component['name']]['latestVersion']) && version_compare($lastSyncData['packages'][$component['name']]['latestVersion'], $component['version'], '>')) {
                 $components[$component['name']]['update'] = true;
             }
         }
         if ($component['type'] === \Magento\Framework\Composer\ComposerInformation::MODULE_PACKAGE_TYPE) {
             $components[$component['name']]['enable'] = $enabledModuleList->has($components[$component['name']]['moduleName']);
             $components[$component['name']]['disable'] = !$components[$component['name']]['enable'];
         } else {
             $components[$component['name']]['enable'] = false;
             $components[$component['name']]['disable'] = false;
         }
         $componentNameParts = explode('/', $component['name']);
         $components[$component['name']]['vendor'] = $componentNameParts[0];
     }
     return new \Zend\View\Model\JsonModel(['success' => true, 'components' => array_values($components), 'total' => count($components), 'lastSyncData' => $lastSyncData]);
 }