Commit c910909d authored by Christian BUFFIN's avatar Christian BUFFIN

Remplacement du plugin interne Appchecks par le plugin LibricielChecks sur...

Remplacement du plugin interne Appchecks par le plugin LibricielChecks sur GitLab LS, ajout du plugin interne CakeTest qui se trouvait sur web-CIL, correction des tests unitaires.
parent c0165790
......@@ -14,6 +14,7 @@
/app/Plugin/Database
/app/Plugin/FusionConv
/app/Plugin/LdapManager
/app/Plugin/LibricielChecks
/app/Plugin/Postgres
/app/Plugin/Translator
/app/webroot/js/bootstrap
......
......@@ -80,10 +80,9 @@ Inflector::rules('plural', array(
*/
CakePlugin::loadAll([
//['bootstrap' => false],
'AuthManager' => ['bootstrap' => true, 'routes' => false],
'Appchecks' => ['bootstrap' => false, 'routes' => false],
'Database' => ['bootstrap' => false, 'routes' => false],
'LibricielChecks' => ['bootstrap' => false, 'routes' => false],
'Postgres' => ['bootstrap' => false, 'routes' => false],
'Translator' => ['bootstrap' => true, 'routes' => false]
]);
......@@ -271,6 +270,7 @@ require_once(APP . DS . 'Lib' . DS . 'basics.php');
CakePlugin::load('Cakeflow', ['bootstrap' => true, 'routes' => false]);
CakePlugin::load('FusionConv', ['bootstrap' => true, 'routes' => false]);
CakePlugin::load('LdapManager', ['bootstrap' => true, 'routes' => false]);
CakePlugin::load('LibricielChecks', ['bootstrap' => false, 'routes' => false]);
//appVersion
$versionFile = file(ROOT . DS . 'VERSION.txt');
......
......@@ -69,6 +69,7 @@ class ChecksShell extends AppShell
}
foreach($results as $key => $result) {
$result += ['value' => null];
if ($result['success'] === true) {
$icon = "\xE2\x9C\x94";
} else {
......
......@@ -28,7 +28,9 @@ class ChecksController extends AppController {
public $uses = ['User'];
public $helpers = [
'Appchecks.Checks'
'Checks' => [
'className' => 'LibricielChecks.LibricielChecks'
]
];
public function index() {
......
......@@ -40,10 +40,6 @@ class OrganisationsController extends AppController
'EtatFiche'
];
public $helpers = [
'Appchecks.Checks'
];
/**
* Accueil de la page, listing des organisations
*
......@@ -408,6 +404,7 @@ class OrganisationsController extends AppController
$this->set('informationsUsers', $result);
} else {
$array_users = null;
$this->set('informationsUsers', []);
}
$roles = $this->Role->find('list', [
......
<?php
/**
* Fonctions utilitaires pour le plugin Appchecks.
*
* PHP 5.3
*
* @package Appchecks
* @subpackage Lib
* @license CeCiLL V2 (http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html)
*/
/**
* Retourne un nombre qui sera plus grand pour une version plus élevée.
*
* @see http://az.php.net/manual/en/function.phpversion.php (Exemple 2: PHP_VERSION_ID)
*
* @param string $version La version pour laquelle obtenir l'id
* @return integer
*/
function version_id( $version ) {
$tokens = explode( '.', $version );
$multipliers = array( 0 => 1000000, 1 => 10000, 2 => 100, 3 => 1 );
$result = 0;
for( $i = 0 ; $i <= 3 ; $i++ ) {
if( isset( $tokens[$i] ) ) {
$result += $multipliers[$i] * $tokens[$i];
}
}
return $result;
}
/**
* Vérifie qu'une version donnée soit au minimum égale à une certaine version
* et soit éventuellement plus petite qu'une version maximale.
*
* @param string $actual La version à tester
* @param string $low La version minimale
* @param string $high La version maximale éventuelle
* @return boolean
*/
function version_difference( $actual, $low, $high = null ) {
$actual = version_id($actual);
$low = version_id($low);
$high = ( is_null($high) ? null : version_id($high) );
$success = ( $actual >= $low );
if ($success === true && is_null($high) === false) {
$success = ( $actual < $high );
}
return $success;
}
?>
This diff is collapsed.
Plugin Appchecks
Contenu du plugin:
I) Modèle: comprend des fonctions pour vérifier différentes choses. N'utilise pas de table.
II) Helper: comprend des fonctions pour afficher les vérifications
Il faudra penser à mettre du CSS, par exemple:
table.checks, table.checks tbody, table.checks tbody th, table.checks tbody td {
background: none;
color: black;
border: 0;
}
table.checks tr th {
background-repeat: no-repeat;
background-position: left top;
padding-left: 20px;
margin-left: 0;
}
table.checks tr.success th {
background-image: url( ../img/icons/tick.png );
}
table.checks tr.error th {
background-image: url( ../img/icons/cross.png );
}
table.checks tr.error th, table.checks tr.error td {
background-color: #fdd;
}
table.checks tr.error td.message {
background-color: white;
}
table.checks { width: 100%; }
table.checks tr th, table.checks tr td { width: 15%; }
table.checks tr td.message { width: 50%; }
\ No newline at end of file
<?php
/**
* AllAppchecksTests file
*
* PHP 5.3
*
* @package Appchecks
* @subpackage Test.Case
*/
CakePlugin::load( 'Appchecks' );
/**
* AllAppchecksTests class
*
* This test group will run all tests.
*
* @package Appchecks
* @subpackage Test.Case
*/
class AllAppchecksTests extends PHPUnit_Framework_TestSuite
{
/**
* Test suite with all test case files.
*
* @return void
*/
public static function suite() {
$suite = new CakeTestSuite( 'All Appchecks tests' );
$suite->addTestDirectoryRecursive( dirname( __FILE__ ).DS.'..'.DS.'Case'.DS );
return $suite;
}
}
?>
\ No newline at end of file
<?php
/**
* BasicsTest file
*
* PHP 5.3
*
* @package Appchecks
* @subpackage Test.Case
* @license CeCiLL V2 (http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html)
*/
require_once dirname(dirname(__DIR__)).DS.'Lib'.DS.'basics.php' ;
/**
* La classe BasicsTest est chargée des tests unitaires des fonctions utilitaires
* du plugin Appchecks.
*
* @package Appchecks
* @subpackage Test.Case
*/
class BasicsTest extends CakeTestCase
{
/**
* Test de la fonction version_id().
*/
public function testVersionId() {
$result = version_id( '5' );
$expected = 5000000;
$this->assertEquals( $expected, $result, var_export( $result, true ) );
$result = version_id( '5.6' );
$expected = 5060000;
$this->assertEquals( $expected, $result, var_export( $result, true ) );
$result = version_id( '5.6.9' );
$expected = 5060900;
$this->assertEquals( $expected, $result, var_export( $result, true ) );
$result = version_id( '5.6.9.1' );
$expected = 5060901;
$this->assertEquals( $expected, $result, var_export( $result, true ) );
}
/**
* Test de la fonction version_difference().
*/
public function testVersionDifference() {
$result = version_difference( '5', '5.1' );
$expected = false;
$this->assertEquals( $expected, $result, var_export( $result, true ) );
$result = version_difference( '5.2', '5.1' );
$expected = true;
$this->assertEquals( $expected, $result, var_export( $result, true ) );
$result = version_difference( '5.2', '5.1', '5.3' );
$expected = true;
$this->assertEquals( $expected, $result, var_export( $result, true ) );
$result = version_difference( '5.2', '5.1', '5.2' );
$expected = false;
$this->assertEquals( $expected, $result, var_export( $result, true ) );
}
}
?>
\ No newline at end of file
<?php
/**
* Code source de la classe ChecksHelperTest.
*
* PHP 5.3
*
* @package Appchecks
* @subpackage Test.Case.View.Helper
* @license CeCiLL V2 (http://www.cecill.info/licences/Licence_CeCILL_V2-fr.html)
*/
App::uses( 'Controller', 'Controller' );
App::uses( 'View', 'View' );
App::uses( 'ChecksHelper', 'Appchecks.View/Helper' );
/**
* La classe ChecksHelperTest contient les tests unitaires de la classe
* ChecksHelper.
*
* @package Appchecks
* @subpackage Test.Case.View.Helper
*/
class ChecksHelperTest extends CakeTestCase
{
/**
* Fixtures utilisés par ces tests unitaires.
*
* @var array
*/
public $fixtures = array(
'core.Apple',
);
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$controller = null;
$this->View = new View( $controller );
$this->Checks = new ChecksHelper( $this->View );
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
unset( $this->View, $this->Checks );
parent::tearDown();
}
/**
* Test de la méthode ChecksHelper::checklist()
*
* @return void
*/
public function testChecklist() {
$elements = ['One' => true, 'Two' => false];
$result = $this->Checks->checklist( $elements, 'drivers' );
$expected = '<ul class="check drivers"><li class="success">One</li><li class="error">Two</li></ul>';
$this->assertEquals( $expected, $result, var_export( $result, true ) );
}
/**
* Test de la méthode ChecksHelper::table()
*
* @return void
*/
public function testTable() {
$elements = ['One' => true, 'Two' => false];
$result = $this->Checks->table( $elements );
$expected = '<div class="table-responsive"><table class="table table-striped table-bordered table-hover table-condensed checks values"><tbody><tr class="col-xs-12"><th>One</th></tr><tr class="col-xs-12"><th>Two</th></tr></tbody></table></div>';
$this->assertEquals( $expected, $result, var_export( $result, true ) );
$elements = ['One' => ['value' => 4, 'message' => 'Foo', 'success' => true], 'Two' => ['value' => 'Bar', 'message' => 'Baz', 'success' => false]];
$result = $this->Checks->table( $elements );
$expected = '<div class="table-responsive"><table class="table table-striped table-bordered table-hover table-condensed checks values"><tbody><tr class="success"><td class="col-xs-1 status success"><span class="fa fa-check"></span> <span class="sr-only">OK</span></td><th class="col-xs-4">One</th><td class="col-xs-3">4</td><td class="col-xs-4">Foo</td></tr><tr class="danger"><td class="col-xs-1 status error"><span class="fa fa-times"></span> <span class="sr-only">Erreur</span></td><th class="col-xs-4">Two</th><td class="col-xs-3">Bar</td><td class="col-xs-4">Baz</td></tr></tbody></table></div>';
$this->assertEquals( $expected, $result, var_export( $result, true ) );
}
}
?>
\ No newline at end of file
<?php
abstract class ChecksCake
{
/**
* Vérification de l'utilisateur et du groupe propriétaires des fichiers
* et dossiers du cache, ainsi que des permissions sur ces fichiers.
*
* @return array
*/
public static function checkCachePermissions()
{
$return = [];
$configNames = (array) Cache::configured();
$user = posix_getpwuid(posix_geteuid());
$group = posix_getgrgid($user['gid']);
$uname = posix_uname();
foreach ($configNames as $configName) {
$config = Cache::config($configName);
if ('file' === strtolower($config['engine'])) {
$dir = $config['settings']['path'];
if ('linux' !== strtolower($uname['sysname'])) {
$success = false;
$message = sprintf('Vérification du propriétaire et des permissions du dossier <var>%s</var> non prévue sur un système %s', $dir, $uname['sysname']);
} else {
$files = [];
$return_var = 0;
$command = "find {$dir} -maxdepth 1 \\( -not -group {$group['name']} \\) -o \\( -not -user {$user['name']} \\) -o \\( -not -perm -u=rw -o -not -perm -g=rw \\) | sort";
exec($command, $files, $return_var);
if (0 == $return_var) {
$success = true === empty($files);
$message = null;
if (false === $success) {
$message = "Des fichiers ou des dossiers du dossier <var>{$dir}</var> n'appartiennent pas au bon utilisateur ou groupe ou n'ont pas les bonnes permissions.<br/>";
$message .= "Il faudrait lancer la commande suivante pour corriger le problème: <code>sudo chown -R {$user['name']}:{$group['name']} {$dir} && sudo chmod o+rwX,g+rwX -R {$dir}</code><br/>";
$message .= "Les fichiers concernés sont: <br/><ul><li>" . implode("</li><li>", $files) . "</li></ul>";
}
} else {
$success = false;
$message = sprintf('Erreur lors de l\'exécution de la commande %s (%d)', $command, $return_var);
}
}
$return[$configName] = array(
'success' => $success,
'message' => $message
);
}
}
return $return;
}
/**
* Effectue des tests d'écriture, de lecture et de suppression du cache,
* pour toutes les configurations définies.
*
* @return array
*/
public static function checkCacheUsage()
{
$cacheKeyBase = get_called_class() . '_' . __FUNCTION__;
$value = time();
$return = [];
$savedCacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', false);
$savedDebug = Configure::read('debug');
Configure::write('debug', 0);
$configNames = Cache::configured();
if (!empty($configNames)) {
foreach ($configNames as $configName) {
if (!Cache::isInitialized($configName)) {
Cache::config($configName);
}
$cacheKey = "{$cacheKeyBase}_{$configName}";
$write = Cache::write($cacheKey, $value, $configName);
$read = ( Cache::read($cacheKey, $configName) == $value );
$delete = Cache::delete($cacheKey, $configName);
$success = ( $write && $read && $delete );
$message = null;
if (!$success) {
$actions = [];
if (!$write) {
$actions[] = 'écriture';
}
if (!$read) {
$actions[] = 'lecture';
}
if (!$delete) {
$actions[] = 'suppression';
}
$actions = implode(', ', $actions);
$message = "Problème(s) rencontré(s) pour \"{$configName}\": {$actions}";
$config = Cache::config($configName);
if ($config['engine'] == 'File') {
$path = preg_replace('/^' . preg_quote(APP, '/') . '/', APP_DIR . DS, $config['settings']['path']);
$message .= "; vérifiez les droits sur le répertoire: {$path}";
}
}
$return[$configName] = array(
'success' => $success,
'message' => $message
);
}
}
Configure::write('Cache.disable', $savedCacheDisable);
Configure::write('debug', $savedDebug);
return $return;
}
/**
* Lecture des chemins de configuration et validation (à minima, les
* valeurs ne pourront pas être vides).
*
* @param array $paths Les chemins à vérifier
* @return array
*/
public static function checkConfigured(array $paths)
{
$return = [];
foreach (Hash::normalize($paths) as $path => $rules) {
$msgstr = null;
$value = Configure::read($path);
if (!empty($rules)) {
foreach ($rules as $rule => $params) {
$params = (array)$params;
if (is_null($msgstr)) {
if (is_array($params['rule']) && isset($params['rule'][0]) == true) {
$rule = $params['rule'][0];
unset($params['rule'][0]);
} elseif (isset($params['rule']) == true) {
$rule = $params['rule'];
unset($params['rule']);
}
$msgstr = ChecksValidator::validate($value, $rule, $params);
}
}
} elseif ($value === null) {
$msgid = 'Missing ini configuration: %s';
$msgstr = sprintf(ChecksTranslate::singular($msgid), $path);
}
$return[$path] = [
'success' => is_null($msgstr),
'value' => $value,
'message' => $msgstr
];
}
return $return;
}
}
<?php
// @info http://php.net/manual/fr/function.clearstatcache.php
App::uses('ChecksTranslate', 'Appchecks.Utility');
abstract class ChecksFilesystem
{
protected static function _return()
{
return [
'success' => null,
'value' => null,
'message' => null
];
}
public static function relpath($path, $root = null)
{
if ($root === null) {
$root = ROOT.DS;
}
return preg_replace('/^' . preg_quote($root, '/') . '/', '', $path);
}
protected static function _expectedPermissions($permissions)
{
$result = [
'r' => mb_strstr($permissions, 'r') !== false,
'w' => mb_strstr($permissions, 'w') !== false,
'x' => mb_strstr($permissions, 'x') !== false
];
// @todo: error
return $result;
}
protected static function _actualPermissions($path)
{
$result = [
'r' => is_readable($path),
'w' => is_writable($path),
'x' => is_executable($path)
];
return $result;
}
/**
* Retourne une représentation lisible par un être humain à partir d'un
* nombre d'octets.
*
* @see http://www.phpfront.com/php/Convert-Bytes-to-corresponding-size/
*
* @param integer $bytes
* @return string
*/
public static function byteSize($bytes)
{
$size = $bytes / 1024;
$format = '%d B';
if ($size < 1024) {
$size = number_format($size, 2);
$format = '%.2f KB';
} else {
if ($size / 1024 < 1024) {
$size = number_format($size / 1024, 2);
$format = '%.2f MB';
} else if ($size / 1024 / 1024 < 1024) {
$size = number_format($size / 1024 / 1024, 2);
$format = '%.2f GB';
} else if ($size / 1024 / 1024 / 1024 < 1024) {
$size = number_format($size / 1024 / 1024 / 1024, 2);
$format = '%.2f TB';
}
}