backend upgrades

This commit is contained in:
Taras Machyshyn
2014-04-11 16:03:48 +03:00
parent 8711376081
commit 22bf45bfc1
10 changed files with 869 additions and 101 deletions

View File

@@ -54,15 +54,27 @@ class Admin extends \Espo\Core\Controllers\Base
return $scheduledJob->getAllNamesOnly();
}
public function actionUploadUpgradePackage()
public function actionUploadUpgradePackage($params, $data)
{
throw new Error("Your EspoCRM version doesn't match for this upgrade package.");
$upgradeManager = new \Espo\Core\UpgradeManager($this->getContainer());
$upgradeId = $upgradeManager->upload($data);
return array(
'version' => '1.1',
'id' => $upgradeId,
'version' => $upgradeManager->getMainFile()['version'],
);
}
public function actionRunUpgrade($params, $data)
{
$upgradeManager = new \Espo\Core\UpgradeManager($this->getContainer());
$upgradeManager->run($data);
return true;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
namespace Espo\Core;
class UpgradeManager extends Upgrades\Base
{
protected $packagePath = 'data/upload/upgrades';
protected $scriptNames = array(
'before' => 'BeforeUpgrade',
'after' => 'AfterUpgrade',
);
}

View File

@@ -0,0 +1,353 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
namespace Espo\Core\Upgrades;
use Espo\Core\Utils\Util,
Espo\Core\Exceptions\Error;
abstract class Base
{
private $container;
private $zipUtil;
private $fileManager;
private $config;
protected $upgradeId = null;
protected $mainFileName = 'main.php';
protected $data;
protected $packagePath = null;
protected $packagePostfix = 'z';
protected $scriptNames = array(
'before' => 'Before',
'after' => 'After',
);
protected $paths = array(
'files' => 'Files',
'scripts' => 'Scripts',
);
public function __construct($container)
{
$this->container = $container;
$this->zipUtil = new \Espo\Core\Utils\File\ZipArchive($container->get('fileManager'));
}
public function __destruct()
{
$this->upgradeId = null;
$this->data = null;
}
protected function getContainer()
{
return $this->container;
}
protected function getZipUtil()
{
return $this->zipUtil;
}
protected function getFileManager()
{
if (!isset($this->fileManager)) {
$this->fileManager = $this->getContainer()->get('fileManager');
}
return $this->fileManager;
}
protected function getConfig()
{
if (!isset($this->config)) {
$this->config = $this->getContainer()->get('config');
}
return $this->config;
}
/**
* Upload an upgrade package
*
* @param [type] $contents
* @return string ID of upgrade process
*/
public function upload($data)
{
$upgradeId = $this->createUpgradeId();
$upgradePath = $this->getUpgradePath();
$upgradePackagePath = $this->getUpgradePath(true);
if (!empty($data)) {
list($prefix, $contents) = explode(',', $data);
$contents = base64_decode($contents);
}
$res = $this->getFileManager()->putContents($upgradePackagePath, $contents);
if ($res === false) {
throw new Error('Could not upload the package.');
}
$res = $this->getZipUtil()->unzip($upgradePackagePath, $upgradePath);
if ($res === false) {
throw new Error('Unnable to unzip the file - '.$upgradePath.'.');
}
if (!$this->isAcceptable()) {
throw new Error("Your EspoCRM version doesn't match for this upgrade package.");
}
return $upgradeId;
}
/**
* Main upgrade process
*
* @param string $upgradeId Upgrade ID, gotten in upload stage
* @return bool
*/
public function run($upgradeId)
{
$this->setUpgradeId($upgradeId);
/* run before install script */
$this->runScript('before');
/* remove files defined in a mainFile */
if (!$this->deleteFiles()) {
throw new Error('Permission denied to delete files.');
}
/* copy files from directory "Files" to EspoCRM files */
if (!$this->copyFiles()) {
throw new Error('Cannot copy files.');
}
$this->getContainer()->get('dataManager')->rebuild();
/* run before install script */
$this->runScript('after');
/* delete unziped files */
$this->deletePackageFiles();
}
protected function createUpgradeId()
{
if (isset($this->upgradeId)) {
throw new Error('Another upgrade process is currently running.');
}
$this->upgradeId = uniqid('upg');
return $this->upgradeId;
}
protected function getUpgradeId()
{
if (!isset($this->upgradeId)) {
throw new Error("Upgrade ID was not specified.");
}
return $this->upgradeId;
}
protected function setUpgradeId($upgradeId)
{
$this->upgradeId = $upgradeId;
}
/**
* Check if version of upgrade is acceptable to current version of EspoCRM
*
* @param string $version
* @return boolean
*/
protected function isAcceptable()
{
$version = $this->getMainFile()['acceptableVersions'];
$currentVersion = $this->getConfig()->get('version');
if (is_string($version)) {
$version = (array) $version;
}
foreach ($version as $strVersion) {
$strVersion = trim($strVersion);
if ($strVersion == $currentVersion) {
return true;
}
$strVersion = str_replace('\\', '', $strVersion);
$strVersion = preg_quote($strVersion);
$strVersion = str_replace('\\*', '+', $strVersion);
if (preg_match('/^'.$strVersion.'/', $currentVersion)) {
return true;
}
}
return false;
}
/**
* Run scripts by type
* @param string $type Ex. "before", "after"
* @return void
*/
protected function runScript($type)
{
$upgradePath = $this->getUpgradePath();
$scriptName = $this->scriptNames[$type];
if (!isset($scriptName)) {
return;
}
$beforeInstallScript = Util::concatPath( array($upgradePath, $this->paths['scripts'], $scriptName) );
if (file_exists($beforeInstallScript)) {
require_once($beforeInstallScript);
$script = new $scriptName();
$script->run($this->getContainer());
}
}
/**
* Get upgrade path
*
* @param string $upgradeId
* @return string
*/
protected function getUpgradePath($isPackage = false)
{
$postfix = $isPackage ? $this->packagePostfix : '';
if (!isset($this->data['upgradePath'])) {
$upgradeId = $this->getUpgradeId();
$this->data['upgradePath'] = Util::concatPath($this->packagePath, $upgradeId);
}
return $this->data['upgradePath'] . $postfix;
}
/**
* Delete files defined in a main file
*
* @return boolen
*/
protected function deleteFiles()
{
$mainFile = $this->getMainFile();
if (!empty($mainFile['delete'])) {
return $this->getFileManager()->remove($mainFile['delete']);
}
return true;
}
/**
* Copy files from upgrade package
*
* @param string $upgradeId
* @return boolean
*/
protected function copyFiles()
{
$upgradePath = $this->getUpgradePath();
$filesPath = Util::concatPath($upgradePath, $this->paths['files']);
return $this->getFileManager()->copy($filesPath, '', true);
}
public function getMainFile()
{
if (!isset($this->data['mainFile'])) {
$upgradePath = $this->getUpgradePath();
$this->data['mainFile'] = $this->getFileManager()->getContents(array($upgradePath, $this->mainFileName));
if (!$this->checkMainFile($this->data['mainFile'])) {
throw new Error('Unsupported package');
}
}
return $this->data['mainFile'];
}
/**
* Check if the main file is correct
*
* @param array $mainFile
* @return boolean
*/
protected function checkMainFile(array $mainFile)
{
$requiredFields = array(
'name',
'version',
'acceptableVersions',
);
foreach ($requiredFields as $fieldName) {
if (empty($mainFile[$fieldName])) {
return false;
}
}
return true;
}
/**
* Delete temporary package files
*
* @return boolean
*/
protected function deletePackageFiles()
{
$upgradePath = $this->getUpgradePath();
$upgradePackagePath = $this->getUpgradePath(true);
$res = $this->getFileManager()->removeInDir($upgradePath, true);
$res &= $this->getFileManager()->removeFile($upgradePackagePath);
return $res;
}
}

View File

@@ -22,7 +22,8 @@
namespace Espo\Core\Utils\File;
use Espo\Core\Utils;
use Espo\Core\Utils,
Espo\Core\Exceptions\Error;
class Manager
{
@@ -41,7 +42,7 @@ class Manager
/**
* Get a list of files in specified directory
* Get a list of files in specified directory
*
* @param string $path string - Folder path, Ex. myfolder
* @param bool | int $recursively - Find files in subfolders
@@ -53,9 +54,9 @@ class Manager
*/
public function getFileList($path, $recursively=false, $filter='', $fileType='all', $isReturnSingleArray = false)
{
if (!file_exists($path)) {
return false;
}
if (!file_exists($path)) {
return false;
}
$result = array();
@@ -81,11 +82,11 @@ class Manager
if ($add) {
if (!empty($filter)) {
if (preg_match('/'.$filter.'/i', $value)) {
$result[] = $value;
$result[] = $value;
}
}
else {
$result[] = $value;
$result[] = $value;
}
}
@@ -100,7 +101,7 @@ class Manager
}
/**
* Convert file list to a single array
* Convert file list to a single array
*
* @param aray $fileList
* @param string $parentDirName
@@ -110,15 +111,15 @@ class Manager
protected function getSingeFileList(array $fileList, $parentDirName = '')
{
$singleFileList = array();
foreach($fileList as $dirName => $fileName) {
foreach($fileList as $dirName => $fileName) {
if (is_array($fileName)) {
if (is_array($fileName)) {
$currentDir = Utils\Util::concatPath($parentDirName, $dirName);
$singleFileList = array_merge($singleFileList, $this->getSingeFileList($fileName, $currentDir));
} else {
$singleFileList[] = Utils\Util::concatPath($parentDirName, $fileName);
}
}
$singleFileList = array_merge($singleFileList, $this->getSingeFileList($fileName, $currentDir));
} else {
$singleFileList[] = Utils\Util::concatPath($parentDirName, $fileName);
}
}
return $singleFileList;
}
@@ -154,7 +155,6 @@ class Manager
return false;
}
/**
* Write data to a file
*
@@ -170,14 +170,14 @@ class Manager
$fullPath = $this->concatPaths($path); //todo remove after changing the params
if ($this->checkCreateFile($fullPath) === false) {
return false;
throw new Error('Permission denied in '. $path);
}
return (file_put_contents($fullPath, $data, $flags, $context) !== FALSE);
return (file_put_contents($fullPath, $data, $flags, $context) !== FALSE);
}
/**
* Save PHP content to file
* Save PHP content to file
*
* @param string | array $path
* @param string $data
@@ -190,7 +190,7 @@ class Manager
}
/**
* Save JSON content to file
* Save JSON content to file
*
* @param string | array $path
* @param string $data
@@ -202,14 +202,14 @@ class Manager
public function putContentsJson($path, $data)
{
if (!Utils\Json::isJSON($data)) {
$data = Utils\Json::encode($data, JSON_PRETTY_PRINT);
}
$data = Utils\Json::encode($data, JSON_PRETTY_PRINT);
}
return $this->putContents($path, $data);
}
/**
* Merge file content and save it to a file
/**
* Merge file content and save it to a file
*
* @param string | array $path
* @param string $content JSON string
@@ -224,16 +224,16 @@ class Manager
$savedDataArray= $this->getArrayData($fileContent);
$newDataArray= $this->getArrayData($content);
$data= Utils\Util::merge($savedDataArray, $newDataArray);
$data= Utils\Util::merge($savedDataArray, $newDataArray);
if ($isJSON) {
$data= Utils\Json::encode($data, JSON_PRETTY_PRINT);
$data= Utils\Json::encode($data, JSON_PRETTY_PRINT);
}
return $this->putContents($path, $data);
return $this->putContents($path, $data);
}
/**
* Merge PHP content and save it to a file
* Merge PHP content and save it to a file
*
* @param string | array $path
* @param string $content
@@ -243,25 +243,25 @@ class Manager
*/
public function mergeContentsPHP($path, $content, $onlyFirstLevel= false)
{
$fileContent = $this->getContents($path);
$fileContent = $this->getContents($path);
$savedDataArray= $this->getArrayData($fileContent);
$newDataArray= $this->getArrayData($content);
if ($onlyFirstLevel) {
foreach($newDataArray as $key => $val) {
foreach($newDataArray as $key => $val) {
$setVal= is_array($val) ? array() : '';
$savedDataArray[$key]= $setVal;
$savedDataArray[$key]= $setVal;
}
}
$data= Utils\Util::merge($savedDataArray, $newDataArray);
$data= Utils\Util::merge($savedDataArray, $newDataArray);
return $this->putContentsPHP($path, $data);
return $this->putContentsPHP($path, $data);
}
/**
* Append the content to the end of the file
* Append the content to the end of the file
*
* @param string | array $path
* @param mixed $data
@@ -330,7 +330,7 @@ class Manager
{
$fullPath = $this->concatPaths($path);
if (file_exists($fullPath)) {
if (file_exists($fullPath) && is_dir($path)) {
return true;
}
@@ -349,9 +349,44 @@ class Manager
return isset($result) ? $result : false;
}
/**
* Copy files from one direcoty to another
*
* @param string $sourcePath
* @param string $destPath
* @param boolean $recursively
* @return boolen
*/
public function copy($sourcePath, $destPath, $recursively = false)
{
$sourcePath = $this->concatPaths($sourcePath);
$destPath = $this->concatPaths($destPath);
if (is_file($sourcePath)) {
$fileList = (array) $sourcePath;
} else {
$fileList = $this->getFileList($sourcePath, $recursively, '', 'all', true);
}
$res = true;
foreach ($fileList as $file) {
$sourceFile = $this->concatPaths(array($sourcePath, $file));
$destFile = $this->concatPaths(array($destPath, $file));
if ($this->checkCreateFile($destFile) === false) {
throw new Error('Permission denied in '. $destFile);
}
$res &= copy($sourceFile, $destFile);
}
return $res;
}
/**
* Create a new file if not exists with all folders in the path.
* Create a new file if not exists with all folders in the path.
*
* @param string $filePath
* @return string
@@ -363,71 +398,57 @@ class Manager
if (file_exists($filePath)) {
if (!in_array($this->getPermissionUtils()->getCurrentPermission($filePath), array($defaultPermissions['file'], $defaultPermissions['dir']))) {
return $this->getPermissionUtils()->setDefaultPermissions($filePath, true);
return $this->getPermissionUtils()->setDefaultPermissions($filePath, true);
}
return true;
}
$pathParts = pathinfo($filePath);
if (!file_exists($pathParts['dirname'])) {
$dirPermission = $defaultPermissions['dir'];
$dirPermission = is_string($dirPermission) ? base_convert($dirPermission,8,10) : $dirPermission;
$dirPermission = $defaultPermissions['dir'];
$dirPermission = is_string($dirPermission) ? base_convert($dirPermission,8,10) : $dirPermission;
if (!mkdir($pathParts['dirname'], $dirPermission, true)) {
$GLOBALS['log']->critical('Permission denied: unable to generate a folder on the server - '.$pathParts['dirname']);
return false;
throw new Error('Permission denied: unable to generate a folder on the server - '.$pathParts['dirname']);
}
}
if (touch($filePath)) {
return $this->getPermissionUtils()->setDefaultPermissions($filePath, true);
return $this->getPermissionUtils()->setDefaultPermissions($filePath, true);
}
return false;
}
/**
* Remove all files in defined directory
* Remove file/files by given path
*
* @param array $filePaths - File paths list
* @param string $dirPath - directory path
* @return bool
*/
public function removeFiles($filePaths, $dirPath = null)
public function removeFile($filePaths, $dirPath = null)
{
if (!is_array($filePaths)) {
$filePaths = (array) $filePaths;
}
$result= true;
$result = true;
foreach ($filePaths as $filePath) {
if (isset($dirPath)) {
$filePath= Utils\Util::concatPath($dirPath, $filePath);
$filePath = Utils\Util::concatPath($dirPath, $filePath);
}
if (file_exists($filePath) && is_file($filePath)) {
$result &= unlink($filePath);
$result &= unlink($filePath);
}
}
return $result;
}
/**
* Remove file by given path
*
* @param array $filePath - File path
* @return bool
*/
public function removeFile($filePath)
{
if (file_exists($filePath) && is_file($filePath)) {
return unlink($filePath);
}
}
/**
* Remove all files in defined directory
/**
* Remove all files inside given path
*
* @param string $dirPath - directory path
* @param bool $removeWithDir - if remove with directory
@@ -436,28 +457,57 @@ class Manager
*/
public function removeInDir($dirPath, $removeWithDir = false)
{
$fileList= $this->getFileList($dirPath, false);
$fileList = $this->getFileList($dirPath, false);
$result = true;
foreach ($fileList as $file) {
$fullPath = Utils\Util::concatPath($dirPath, $file);
if (is_dir($fullPath)) {
$result &= $this->removeInDir($fullPath, true);
} else {
$result &= unlink($fullPath);
}
}
$result = true;
foreach ($fileList as $file) {
$fullPath = Utils\Util::concatPath($dirPath, $file);
if (is_dir($fullPath)) {
$result &= $this->removeInDir($fullPath, true);
} else {
$result &= unlink($fullPath);
}
}
if ($removeWithDir) {
rmdir($dirPath);
}
if ($removeWithDir) {
rmdir($dirPath);
}
return $result;
}
/**
* Remove items (files or directories)
*
* @param string | array $items
* @param string $dirPath
* @return boolean
*/
public function remove($items, $dirPath = null)
{
if (!is_array($items)) {
$items = (array) $items;
}
$result = true;
foreach ($items as $item) {
if (isset($dirPath)) {
$item = Utils\Util::concatPath($dirPath, $item);
}
if (is_dir($item)) {
$result = $this->removeInDir($item, true);
} else {
$result = $this->removeFile($item);
}
}
return $result;
}
/** //TODO remove
* Get an array data (if JSON convert to array)
/** //TODO remove
* Get an array data (if JSON convert to array)
*
* @param mixed $data - can be JSON, array
*
@@ -466,18 +516,18 @@ class Manager
protected function getArrayData($data)
{
if (is_array($data)) {
return $data;
return $data;
}
else if (Utils\Json::isJSON($data)) {
return Utils\Json::decode($data, true);
}
return Utils\Json::decode($data, true);
}
return array();
}
/**
* Get a filename without the file extension
* Get a filename without the file extension
*
* @param string $filename
* @param string $ext - extension, ex. '.json'
@@ -491,22 +541,22 @@ class Manager
}
else {
if (substr($ext, 0, 1)!='.') {
$ext= '.'.$ext;
$ext= '.'.$ext;
}
if (substr($fileName, -(strlen($ext)))==$ext) {
$fileName= substr($fileName, 0, -(strlen($ext)));
}
}
}
$exFileName = explode('/', Utils\Util::toFormat($fileName, '/'));
$exFileName = explode('/', Utils\Util::toFormat($fileName, '/'));
return end($exFileName);
}
/**
* Get a directory name from the path
* Get a directory name from the path
*
* @param string $path
* @param bool $isFullPath
@@ -528,7 +578,7 @@ class Manager
/**
* Return content of PHP file
* Return content of PHP file
*
* @param string $varName - name of variable which contains the content
* @param array $content
@@ -538,10 +588,10 @@ class Manager
public function getPHPFormat($content)
{
if (empty($content)) {
return false;
return false;
}
return '<?php
return '<?php
return '.var_export($content, true).';

View File

@@ -0,0 +1,81 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
namespace Espo\Core\Utils\File;
use Espo\Core\Exceptions\Error;
class ZipArchive
{
private $fileManager;
public function __construct(Manager $fileManager = null)
{
if (!isset($fileManager)) {
$fileManager = new Manager();
}
$this->fileManager = $fileManager;
}
protected function getFileManager()
{
return $this->fileManager;
}
public function zip($sourcePath, $file)
{
}
/**
* Unzip archive
*
* @param string $file Path to .zip file
* @param [type] $destinationPath
* @return bool
*/
public function unzip($file, $destinationPath)
{
if (!class_exists('\ZipArchive')) {
throw new Error("Class ZipArchive does not installed. Cannot unzip the file.");
}
$zip = new \ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$this->getFileManager()->mkdir($destinationPath);
$zip->extractTo($destinationPath);
$zip->close();
return true;
}
return false;
}
}

View File

@@ -77,7 +77,7 @@ class RotatingFileHandler extends StreamHandler
$logFilesToBeRemoved = array_slice($logFiles, $this->maxFiles);
$this->getFileManager()->removeFiles($logFilesToBeRemoved, $dirPath);
$this->getFileManager()->removeFile($logFilesToBeRemoved, $dirPath);
}
}

View File

@@ -171,13 +171,21 @@ class Util
/**
* Get a full path of the file
*
* @param string $folderPath - Folder path, Ex. myfolder
* @param string | array $folderPath - Folder path, Ex. myfolder
* @param string $filePath - File path, Ex. file.json
*
* @return string
*/
public static function concatPath($folderPath, $filePath='')
public static function concatPath($folderPath, $filePath = null)
{
if (is_array($folderPath)) {
$fullPath = '';
foreach ($folderPath as $path) {
$fullPath = static::concatPath($fullPath, $path);
}
return $fullPath;
}
if (empty($filePath)) {
return $folderPath;
}
@@ -185,12 +193,10 @@ class Util
return $filePath;
}
else {
if (substr($folderPath, -1) == static::getSeparator()) {
return $folderPath . $filePath;
}
return $folderPath . static::getSeparator() . $filePath;
if (substr($folderPath, -1) == static::getSeparator()) {
return $folderPath . $filePath;
}
return $folderPath . static::getSeparator() . $filePath;
}

View File

@@ -0,0 +1,28 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
namespace tests\Espo\Core\Upgrades;
class Base extends \Espo\Core\Upgrades\Base
{
protected $packagePath = 'tests/testData/Upgrades/data/upload/upgrades';
}

View File

@@ -0,0 +1,194 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
namespace tests\Espo\Core\Upgrades;
use tests\ReflectionHelper;
class BaseTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected $objects;
protected function setUp()
{
$this->objects['container'] = $this->getMockBuilder('\Espo\Core\Container')->disableOriginalConstructor()->getMock();
$this->objects['config'] = $this->getMockBuilder('\Espo\Core\Utils\Config')->disableOriginalConstructor()->getMock();
$this->objects['fileManager'] = $this->getMockBuilder('\Espo\Core\Utils\File\Manager')->disableOriginalConstructor()->getMock();
$map = array(
array('config', $this->objects['config']),
array('fileManager', $this->objects['fileManager']),
);
$this->objects['container']
->expects($this->any())
->method('get')
->will($this->returnValueMap($map));
$this->object = new Base( $this->objects['container'] );
$this->reflection = new ReflectionHelper($this->object);
$this->reflection->setProperty('upgradeId', 'ngkdf54n566n45');
}
protected function tearDown()
{
$this->object = NULL;
}
public function testCreateUpgradeIdWithExists()
{
$this->setExpectedException('\Espo\Core\Exceptions\Error');
$upgradeId = $this->reflection->invokeMethod('createUpgradeId', array());
$this->assertEquals( $upgradeId, $this->reflection->invokeMethod('getUpgradeId', array()) );
}
public function testCreateUpgradeId()
{
$upgradeId = $this->reflection->setProperty('upgradeId', null);
$upgradeId = $this->reflection->invokeMethod('createUpgradeId', array());
$this->assertEquals( $upgradeId, $this->reflection->invokeMethod('getUpgradeId', array()) );
}
public function testGetUpgradeId()
{
$this->setExpectedException('\Espo\Core\Exceptions\Error');
$this->reflection->setProperty('upgradeId', null);
$this->reflection->invokeMethod('getUpgradeId', array());
}
public function testGetMainFileIncorrect()
{
$this->setExpectedException('\Espo\Core\Exceptions\Error');
$mainFile = array(
'name' => 'Upgrade 1.0-b3 to 1.0-b4',
);
$this->objects['fileManager']
->expects($this->once())
->method('getContents')
->will($this->returnValue($mainFile));
$this->reflection->invokeMethod('getMainFile', array());
}
public function testGetMainFile()
{
$mainFile = array(
'name' => 'Upgrade 1.0-b3 to 1.0-b4',
'version' => '1.0-b4',
'acceptableVersions' =>array(
'1.0-b3',
),
'releaseDate' => '2014-04-08',
'author' => 'EspoCRM',
'description' => 'Upgrade 1.0-b3 to 1.0-b4',
);
$this->objects['fileManager']
->expects($this->once())
->method('getContents')
->will($this->returnValue($mainFile));
$this->assertEquals( $mainFile, $this->reflection->invokeMethod('getMainFile', array()) );
}
/**
* @dataProvider acceptableData
*/
public function testIsAcceptable($version)
{
$this->objects['config']
->expects($this->once())
->method('get')
->will($this->returnValue('11.5.2'));
$this->reflection->setProperty('data', array('mainFile' => array('acceptableVersions' => $version)));
$this->assertTrue( $this->reflection->invokeMethod('isAcceptable', array()) );
}
public function acceptableData()
{
return array(
array( '11.5.2' ),
array( array('11.5.2') ),
array( array('1.4', '11.5.2') ),
array( '11.*' ),
array( '11\.*' ),
array( '11.5*' ),
);
}
/**
* @dataProvider acceptableDataFalse
*/
public function testIsAcceptableFalse($version)
{
$this->objects['config']
->expects($this->once())
->method('get')
->will($this->returnValue('11.5.2'));
$this->reflection->setProperty('data', array('mainFile' => array('acceptableVersions' => $version)));
$this->assertFalse( $this->reflection->invokeMethod('isAcceptable', array()) );
}
public function acceptableDataFalse()
{
return array(
array( '1.*' ),
);
}
public function testGetUpgradePath()
{
$upgradeId = $this->reflection->invokeMethod('getUpgradeId', array());
$upgradePath = 'tests/testData/Upgrades/data/upload/upgrades/'.$upgradeId;
$this->assertEquals( $upgradePath, $this->reflection->invokeMethod('getUpgradePath', array()) );
$postfix = $this->reflection->getProperty('packagePostfix');
$this->assertEquals( $upgradePath.$postfix, $this->reflection->invokeMethod('getUpgradePath', array(true)) );
}
}
?>

View File

@@ -133,6 +133,14 @@ class UtilTest extends \PHPUnit_Framework_TestCase
$result= 'dir1/dir2/file1.json';
$this->assertEquals($result, Util::concatPath('dir1/dir2/file1.json'));
$input = array('dir1/dir2', 'file1.json');
$result= 'dir1/dir2/file1.json';
$this->assertEquals($result, Util::concatPath($input));
$input = array('dir1/', 'dir2', 'file1.json');
$result = 'dir1/dir2/file1.json';
$this->assertEquals($result, Util::concatPath($input));
}