[2931] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
| 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
| 7 | # Licensed under the GPL version 2.0 license. |
---|
| 8 | # See LICENSE file or |
---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
| 10 | # |
---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
| 12 | |
---|
| 13 | use Behat\MinkExtension\Context\RawMinkContext; |
---|
| 14 | use Behat\Behat\Event\SuiteEvent; |
---|
| 15 | use Behat\Behat\Event\ScenarioEvent; |
---|
| 16 | use Behat\Gherkin\Node\TableNode; |
---|
| 17 | |
---|
| 18 | include_once __DIR__.'/autoload.php'; |
---|
| 19 | |
---|
| 20 | class DbContext extends RawMinkContext |
---|
| 21 | { |
---|
| 22 | private static $conf_loaded = false; |
---|
| 23 | private static $con = null; |
---|
| 24 | public static $prefix = 'dc_'; |
---|
| 25 | |
---|
| 26 | public function __construct($parameters) { |
---|
| 27 | $this->parameters = $parameters; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * @Given /^a user:$/ |
---|
| 32 | */ |
---|
| 33 | public function aUser(TableNode $table) { |
---|
| 34 | foreach ($table->getHash() as $user) { |
---|
| 35 | $this->last_id = self::addUser($user); |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | /* |
---|
| 40 | /* ORM methods |
---|
| 41 | **/ |
---|
| 42 | /** |
---|
| 43 | * @BeforeSuite |
---|
| 44 | */ |
---|
| 45 | public static function prepareDB(SuiteEvent $event) { |
---|
| 46 | $parameters = $event->getContextParameters(); |
---|
| 47 | |
---|
| 48 | if (empty($parameters['config_file']) || !is_readable($parameters['config_file'])) { |
---|
| 49 | throw new Exception(sprintf('Config file %s does not exist or not readable', $parameters['config_file'])); |
---|
| 50 | } |
---|
| 51 | if (empty($parameters['sql_init_file']) || !is_readable($parameters['sql_init_file'])) { |
---|
| 52 | throw new Exception(sprintf('sql init file %s does not exist or not readable', $parameters['sql_init_file'])); |
---|
| 53 | } |
---|
| 54 | self::getConnection($parameters); |
---|
| 55 | self::executeSqlFile($parameters['sql_init_file']); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * @AfterScenario |
---|
| 60 | */ |
---|
| 61 | public static function cleanDB(ScenarioEvent $event) { |
---|
| 62 | $parameters = $event->getContext()->parameters; |
---|
| 63 | |
---|
| 64 | if (empty($parameters['config_file']) || !is_readable($parameters['config_file'])) { |
---|
| 65 | throw new Exception(sprintf('Config file %s does not exist or not readable', $parameters['config_file'])); |
---|
| 66 | } |
---|
| 67 | self::getConnection($parameters); |
---|
| 68 | |
---|
| 69 | if (empty($parameters['sql_cleanup_file']) && !is_readable($parameters['sql_cleanup_file'])) { |
---|
| 70 | throw new Exception(sprintf('sql cleanup file %s does not exist or not readable', $parameters['sql_cleanup_file'])); |
---|
| 71 | } |
---|
| 72 | self::executeSqlFile($parameters['sql_cleanup_file']); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | private function addUser(array $params) { |
---|
| 76 | self::getConnection($this->parameters); |
---|
| 77 | if (empty($params['username']) || empty($params['password'])) { |
---|
| 78 | throw new Exception('Username and Password for user are mandatory'."\n"); |
---|
| 79 | } |
---|
| 80 | $strReq = 'SELECT count(1) FROM '.self::$prefix.'user'; |
---|
| 81 | $strReq .= ' WHERE user_id = \''.self::$con->escape($params['username']).'\''; |
---|
| 82 | if ((int) self::$con->select($strReq)->f(0)==0) { |
---|
| 83 | $user = self::$con->openCursor(self::$prefix . 'user'); |
---|
| 84 | $user->user_id = $params['username']; |
---|
| 85 | $user->user_pwd = \crypt::hmac(DC_MASTER_KEY,$params['password']); |
---|
| 86 | $user->user_super = 1; |
---|
| 87 | $user->insert(); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * Create a database connexion if none exists |
---|
| 93 | */ |
---|
| 94 | private static function getConnection($parameters) { |
---|
| 95 | if (!self::$conf_loaded) { |
---|
| 96 | // @TODO : find a better way to include conf without define DC_RC_PATH |
---|
| 97 | define('DC_RC_PATH', $parameters['config_file']); |
---|
| 98 | |
---|
| 99 | include($parameters['config_file']); |
---|
| 100 | self::$conf_loaded = true; |
---|
| 101 | self::$prefix = DC_DBPREFIX; |
---|
| 102 | |
---|
| 103 | self::$con = \dbLayer::init(DC_DBDRIVER,DC_DBHOST,DC_DBNAME,DC_DBUSER,DC_DBPASSWORD,DC_DBPERSIST); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | private static function executeSqlFile($file) { |
---|
| 108 | $queries = file($file); |
---|
| 109 | if (!empty($queries)) { |
---|
| 110 | try { |
---|
| 111 | foreach ($queries as $query) { |
---|
| 112 | if (!empty($query)) { |
---|
| 113 | self::$con->execute($query); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | } catch (\Exception $e) { |
---|
| 117 | // @TODO : make something ; exception thrown "database schema has changed (17)" ??? |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|