Dotclear

source: features/bootstrap/DbContext.php @ 2984:b81d45a0e150

Revision 2984:b81d45a0e150, 4.8 KB checked in by Nicolas <nikrou77@…>, 11 years ago (diff)

Behat - add a new configuration key in behat.yml file : user_id_to_not_delete
So you can use an existing dotclear installation and not delete superadmin user

Use cookie session name provide in dotclear configuration file

Line 
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
13use Behat\MinkExtension\Context\RawMinkContext;
14use Behat\Behat\Event\SuiteEvent;
15use Behat\Behat\Event\ScenarioEvent;
16use Behat\Gherkin\Node\TableNode;
17
18include_once __DIR__.'/autoload.php';
19
20class DbContext extends RawMinkContext
21{
22    private static $conf_loaded = false;
23    private static $con = null;
24    private static $session_name = null;
25    public static $prefix = 'dc_';
26
27    public function __construct($parameters) {
28        $this->parameters = $parameters;
29    }
30
31    public function getSessionName($parameters) {
32        if (!self::$conf_loaded) {
33            self::getConnection($parameters);
34        }
35
36        return self::$session_name;
37    }
38
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
48    /*
49    /* ORM methods
50    **/
51    /**
52     * @BeforeSuite
53     */
54    public static function prepareDB(SuiteEvent $event) {
55        $parameters = $event->getContextParameters();
56
57        if (empty($parameters['config_file']) || !is_readable($parameters['config_file'])) {
58            throw new Exception(sprintf('Config file %s does not exist or not readable', $parameters['config_file']));
59        }
60        if (empty($parameters['sql_init_file'])  || !is_readable($parameters['sql_init_file'])) {
61            throw new Exception(sprintf('sql init file %s does not exist or not readable', $parameters['sql_init_file']));
62        }
63        self::getConnection($parameters);
64        self::executeSqlFile($parameters['sql_init_file']);
65    }
66
67    /**
68     * @AfterScenario
69     */
70    public static function cleanDB(ScenarioEvent $event) {
71        $parameters = $event->getContext()->parameters;
72
73        if (empty($parameters['config_file']) || !is_readable($parameters['config_file'])) {
74            throw new Exception(sprintf('Config file %s does not exist or not readable', $parameters['config_file']));
75        }
76        self::getConnection($parameters);
77
78        if (empty($parameters['sql_cleanup_file']) && !is_readable($parameters['sql_cleanup_file'])) {
79            throw new Exception(sprintf('sql cleanup file %s does not exist or not readable', $parameters['sql_cleanup_file']));
80        }
81        if (!empty($parameters['user_id_to_not_delete'])) {
82            $replace_user_id  = $parameters['user_id_to_not_delete'];
83        } else {
84            $replace_user_id = null;
85        }
86        self::executeSqlFile($parameters['sql_cleanup_file'], $replace_user_id);
87   }
88
89    private function addUser(array $params) {
90        self::getConnection($this->parameters);
91        if (empty($params['username']) || empty($params['password'])) {
92            throw new Exception('Username and Password for user are mandatory'."\n");
93        }
94        $strReq = 'SELECT count(1) FROM '.self::$prefix.'user';
95        $strReq .= ' WHERE user_id = \''.self::$con->escape($params['username']).'\'';
96        if ((int) self::$con->select($strReq)->f(0)==0) {
97            $user = self::$con->openCursor(self::$prefix . 'user');
98            $user->user_id = $params['username'];
99               $user->user_pwd = \crypt::hmac(DC_MASTER_KEY,$params['password']);
100            $user->user_super = 1;
101            $user->insert();
102        }
103    }
104
105    /**
106     *  Create a database connexion if none exists
107     */
108    private static function getConnection($parameters) {
109        if (!self::$conf_loaded) {
110            // @TODO : find a better way to include conf without define DC_RC_PATH
111            define('DC_RC_PATH', $parameters['config_file']);
112
113            include($parameters['config_file']);
114            self::$conf_loaded = true;
115            self::$prefix = DC_DBPREFIX;
116            self::$session_name = DC_SESSION_NAME;
117
118            self::$con = \dbLayer::init(DC_DBDRIVER,DC_DBHOST,DC_DBNAME,DC_DBUSER,DC_DBPASSWORD,DC_DBPERSIST);
119        }
120    }
121
122    private static function executeSqlFile($file, $replace_user_id=null) {
123        $queries = file($file);
124        if ($replace_user_id) {
125            $queries = str_replace('__USER_ID__', $replace_user_id, $queries);
126        }
127        if (!empty($queries)) {
128            try {
129                foreach ($queries as $query) {
130                    if (!empty($query)) {
131                        self::$con->execute($query);
132                    }
133                }
134            } catch (\Exception $e) {
135                // @TODO : make something ; exception thrown "database schema has changed (17)" ???
136            }
137        }
138    }
139}
Note: See TracBrowser for help on using the repository browser.

Sites map