protected function setUp() { $this->connectionFactory = $this->getMock('Magento\Setup\Module\ConnectionFactory', [], [], '', false); $this->connection = $this->getMockForAbstractClass('Magento\Framework\DB\Adapter\AdapterInterface'); $this->connectionFactory->expects($this->any())->method('create')->willReturn($this->connection); $this->dbValidator = new DbValidator($this->connectionFactory); }
/** * Checks Database Connection * * @param string $dbName * @param string $dbHost * @param string $dbUser * @param string $dbPass * @return boolean * @throws \Magento\Setup\Exception */ public function checkDatabaseConnection($dbName, $dbHost, $dbUser, $dbPass = '') { $connection = $this->connectionFactory->create([ 'dbname' => $dbName, 'host' => $dbHost, 'username' => $dbUser, 'password' => $dbPass, 'active' => true, ]); if (!$connection) { throw new \Magento\Setup\Exception('Database connection failure.'); } $mysqlVersion = $connection->fetchOne('SELECT version()'); if ($mysqlVersion) { if (preg_match('/^([0-9\.]+)/', $mysqlVersion, $matches)) { if (isset($matches[1]) && !empty($matches[1])) { if (version_compare($matches[1], Installer::MYSQL_VERSION_REQUIRED) < 0) { throw new \Magento\Setup\Exception( 'Sorry, but we support MySQL version '. Installer::MYSQL_VERSION_REQUIRED . ' or later.' ); } } } } return true; }
/** * Checks Database Connection * * @param string $dbName * @param string $dbHost * @param string $dbUser * @param string $dbPass * @return boolean * @throws \Magento\Setup\Exception */ public function checkDatabaseConnection($dbName, $dbHost, $dbUser, $dbPass = '') { // establish connection to information_schema view to retrieve information about user and table privileges $connection = $this->connectionFactory->create([ ConfigOptionsListConstants::KEY_NAME => 'information_schema', ConfigOptionsListConstants::KEY_HOST => $dbHost, ConfigOptionsListConstants::KEY_USER => $dbUser, ConfigOptionsListConstants::KEY_PASSWORD => $dbPass, ConfigOptionsListConstants::KEY_ACTIVE => true, ]); if (!$connection) { throw new \Magento\Setup\Exception('Database connection failure.'); } $mysqlVersion = $connection->fetchOne('SELECT version()'); if ($mysqlVersion) { if (preg_match('/^([0-9\.]+)/', $mysqlVersion, $matches)) { if (isset($matches[1]) && !empty($matches[1])) { if (version_compare($matches[1], Installer::MYSQL_VERSION_REQUIRED) < 0) { throw new \Magento\Setup\Exception( 'Sorry, but we support MySQL version ' . Installer::MYSQL_VERSION_REQUIRED . ' or later.' ); } } } } return $this->checkDatabasePrivileges($connection, $dbName); }
/** * Validate admin user name and email. * * @param array $data * @return void * @throws \Exception */ public function validate(array $data) { try { $dbConnection = $this->connectionFactory->create([ConfigOption::KEY_NAME => $data[ConfigOption::INPUT_KEY_DB_NAME], ConfigOption::KEY_HOST => $data[ConfigOption::INPUT_KEY_DB_HOST], ConfigOption::KEY_USER => $data[ConfigOption::INPUT_KEY_DB_USER], ConfigOption::KEY_PASSWORD => $data[ConfigOption::INPUT_KEY_DB_PASSWORD], ConfigOption::KEY_PREFIX => $data[ConfigOption::INPUT_KEY_DB_PREFIX]]); $userName = $data[AdminAccount::KEY_USER]; $userEmail = $data[AdminAccount::KEY_EMAIL]; $userTable = $dbConnection->getTableName('admin_user'); $result = $dbConnection->fetchRow("SELECT user_id, username, email FROM {$userTable} WHERE username = :username OR email = :email", ['username' => $userName, 'email' => $userEmail]); $setup = $this->setupFactory->create(); $adminAccount = $this->adminAccountFactory->create($setup, [AdminAccount::KEY_USER => $userName, AdminAccount::KEY_EMAIL => $userEmail]); } catch (\Exception $e) { return; } if (!empty($result)) { $adminAccount->validateUserMatches($result['username'], $result['email']); } }
/** * Deletes the database and creates it again * * @return void */ public function cleanupDb() { // stops cleanup if configuration does not exist if ($this->deploymentConfig->isAvailable()) { $config = $this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT); if ($config) { try { $connection = $this->connectionFactory->create($config); if (!$connection) { $this->log->log("Can't create connection to database - skipping database cleanup"); } } catch (\Exception $e) { $this->log->log($e->getMessage() . ' - skipping database cleanup'); return; } $dbName = $connection->quoteIdentifier($config[ConfigOptionsListConstants::KEY_NAME]); $this->log->log("Cleaning up database {$dbName}"); $connection->query("DROP DATABASE IF EXISTS {$dbName}"); $connection->query("CREATE DATABASE IF NOT EXISTS {$dbName}"); return; } } $this->log->log('No database connection defined - skipping database cleanup'); }
/** * Deletes the database and creates it again * * @return void */ public function cleanupDb() { // stops cleanup if app/etc/config.php does not exist if ($this->deploymentConfig->isAvailable()) { $dbConfig = new DbConfig($this->deploymentConfig->getSegment(DbConfig::CONFIG_KEY)); $config = $dbConfig->getConnection(\Magento\Framework\App\Resource\Config::DEFAULT_SETUP_CONNECTION); if ($config) { try { $connection = $this->connectionFactory->create($config); if (!$connection) { $this->log->log("Can't create connection to database - skipping database cleanup"); } } catch (\Exception $e) { $this->log->log($e->getMessage() . ' - skipping database cleanup'); return; } $dbName = $connection->quoteIdentifier($config['dbname']); $this->log->log("Recreating database {$dbName}"); $connection->query("DROP DATABASE IF EXISTS {$dbName}"); $connection->query("CREATE DATABASE IF NOT EXISTS {$dbName}"); return; } } $this->log->log('No database connection defined - skipping database cleanup'); }
/** * @param array $config * @dataProvider dataProviderCreateNoActiveConfig * @expectedException \InvalidArgumentException * @expectedExceptionMessage MySQL adapter: Missing required configuration option 'host' */ public function testCreateNoActiveConfig($config) { $this->connectionFactory->create($config); }