[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; |
---|
[2984] | 24 | private static $session_name = null; |
---|
[2931] | 25 | public static $prefix = 'dc_'; |
---|
| 26 | |
---|
| 27 | public function __construct($parameters) { |
---|
| 28 | $this->parameters = $parameters; |
---|
| 29 | } |
---|
| 30 | |
---|
[2984] | 31 | public function getSessionName($parameters) { |
---|
| 32 | if (!self::$conf_loaded) { |
---|
| 33 | self::getConnection($parameters); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | return self::$session_name; |
---|
| 37 | } |
---|
| 38 | |
---|
[2931] | 39 | /** |
---|
| 40 | * @Given /^a user:$/ |
---|
| 41 | */ |
---|
| 42 | public function aUser(TableNode $table) { |
---|
| 43 | foreach ($table->getHash() as $user) { |
---|
| 44 | $this->last_id = self::addUser($user); |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
[2986] | 48 | /** |
---|
| 49 | * @Given /^a blog:$/ |
---|
| 50 | */ |
---|
| 51 | public function aBlog(TableNode $table) { |
---|
| 52 | foreach ($table->getHash() as $blog) { |
---|
| 53 | $this->last_id = self::addBlog($blog); |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
[2931] | 57 | /* |
---|
| 58 | /* ORM methods |
---|
| 59 | **/ |
---|
| 60 | /** |
---|
| 61 | * @BeforeSuite |
---|
| 62 | */ |
---|
| 63 | public static function prepareDB(SuiteEvent $event) { |
---|
| 64 | $parameters = $event->getContextParameters(); |
---|
| 65 | |
---|
| 66 | if (empty($parameters['config_file']) || !is_readable($parameters['config_file'])) { |
---|
| 67 | throw new Exception(sprintf('Config file %s does not exist or not readable', $parameters['config_file'])); |
---|
| 68 | } |
---|
| 69 | if (empty($parameters['sql_init_file']) || !is_readable($parameters['sql_init_file'])) { |
---|
| 70 | throw new Exception(sprintf('sql init file %s does not exist or not readable', $parameters['sql_init_file'])); |
---|
| 71 | } |
---|
| 72 | self::getConnection($parameters); |
---|
| 73 | self::executeSqlFile($parameters['sql_init_file']); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * @AfterScenario |
---|
| 78 | */ |
---|
| 79 | public static function cleanDB(ScenarioEvent $event) { |
---|
| 80 | $parameters = $event->getContext()->parameters; |
---|
| 81 | |
---|
| 82 | if (empty($parameters['config_file']) || !is_readable($parameters['config_file'])) { |
---|
| 83 | throw new Exception(sprintf('Config file %s does not exist or not readable', $parameters['config_file'])); |
---|
| 84 | } |
---|
| 85 | self::getConnection($parameters); |
---|
| 86 | |
---|
| 87 | if (empty($parameters['sql_cleanup_file']) && !is_readable($parameters['sql_cleanup_file'])) { |
---|
| 88 | throw new Exception(sprintf('sql cleanup file %s does not exist or not readable', $parameters['sql_cleanup_file'])); |
---|
| 89 | } |
---|
[2984] | 90 | if (!empty($parameters['user_id_to_not_delete'])) { |
---|
| 91 | $replace_user_id = $parameters['user_id_to_not_delete']; |
---|
| 92 | } else { |
---|
| 93 | $replace_user_id = null; |
---|
| 94 | } |
---|
| 95 | self::executeSqlFile($parameters['sql_cleanup_file'], $replace_user_id); |
---|
[2986] | 96 | } |
---|
[2931] | 97 | |
---|
| 98 | private function addUser(array $params) { |
---|
| 99 | self::getConnection($this->parameters); |
---|
| 100 | if (empty($params['username']) || empty($params['password'])) { |
---|
| 101 | throw new Exception('Username and Password for user are mandatory'."\n"); |
---|
| 102 | } |
---|
| 103 | $strReq = 'SELECT count(1) FROM '.self::$prefix.'user'; |
---|
| 104 | $strReq .= ' WHERE user_id = \''.self::$con->escape($params['username']).'\''; |
---|
| 105 | if ((int) self::$con->select($strReq)->f(0)==0) { |
---|
| 106 | $user = self::$con->openCursor(self::$prefix . 'user'); |
---|
| 107 | $user->user_id = $params['username']; |
---|
| 108 | $user->user_pwd = \crypt::hmac(DC_MASTER_KEY,$params['password']); |
---|
| 109 | $user->user_super = 1; |
---|
| 110 | $user->insert(); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
[2986] | 114 | private function addBlog(array $params) { |
---|
| 115 | self::getConnection($this->parameters); |
---|
| 116 | if (empty($params['blog_id']) || empty($params['blog_name']) || empty($params['blog_url'])) { |
---|
| 117 | throw new Exception('blog_id, blog_name and blog_url for blog are mandatory'."\n"); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | $strReq = 'SELECT count(1) FROM '.self::$prefix.'blog'; |
---|
| 121 | $strReq .= ' WHERE blog_id = \''.self::$con->escape($params['blog_id']).'\''; |
---|
| 122 | if ((int) self::$con->select($strReq)->f(0)==0) { |
---|
| 123 | $blog = self::$con->openCursor(self::$prefix . 'blog'); |
---|
| 124 | $blog->blog_id = $params['blog_id']; |
---|
| 125 | $blog->blog_name = $params['blog_name']; |
---|
| 126 | $blog->blog_url = $params['blog_url']; |
---|
| 127 | $blog->blog_creadt = date('Y-m-d H:i:s'); |
---|
| 128 | $blog->blog_upddt = date('Y-m-d H:i:s'); |
---|
| 129 | $blog->blog_uid = md5(uniqid()); |
---|
| 130 | $blog->insert(); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
[2931] | 134 | /** |
---|
| 135 | * Create a database connexion if none exists |
---|
| 136 | */ |
---|
| 137 | private static function getConnection($parameters) { |
---|
| 138 | if (!self::$conf_loaded) { |
---|
| 139 | // @TODO : find a better way to include conf without define DC_RC_PATH |
---|
| 140 | define('DC_RC_PATH', $parameters['config_file']); |
---|
| 141 | |
---|
| 142 | include($parameters['config_file']); |
---|
| 143 | self::$conf_loaded = true; |
---|
| 144 | self::$prefix = DC_DBPREFIX; |
---|
[2984] | 145 | self::$session_name = DC_SESSION_NAME; |
---|
[2931] | 146 | |
---|
| 147 | self::$con = \dbLayer::init(DC_DBDRIVER,DC_DBHOST,DC_DBNAME,DC_DBUSER,DC_DBPASSWORD,DC_DBPERSIST); |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
[2984] | 151 | private static function executeSqlFile($file, $replace_user_id=null) { |
---|
[2931] | 152 | $queries = file($file); |
---|
[2984] | 153 | if ($replace_user_id) { |
---|
| 154 | $queries = str_replace('__USER_ID__', $replace_user_id, $queries); |
---|
| 155 | } |
---|
[2931] | 156 | if (!empty($queries)) { |
---|
| 157 | try { |
---|
| 158 | foreach ($queries as $query) { |
---|
| 159 | if (!empty($query)) { |
---|
| 160 | self::$con->execute($query); |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | } catch (\Exception $e) { |
---|
| 164 | // @TODO : make something ; exception thrown "database schema has changed (17)" ??? |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | } |
---|