librenms/tests/DBSetupTest.php
Tony Murray e18f4522d5
Update to Laravel 5.7 (PHP 7.3 support) (#9800)
* Move assets to 5.7 location

* Add 5.7 SVGs

* add cache data dir

* update QUEUE_DRIVER -> QUEUE_CONNECTION

* Update trusted proxy config

* update composer.json

* 5.5 command loading

* @php and @endphp can't be inline

* Laravel 5.6 logging, Nice!

* Update blade directives

* improved redirects

* remove unneeded service providers

* Improved debugbar loading

* no need to emulate renderable exceptions anymore

* merge updated 5.7 files (WIP)

* Enable CSRF

* database_path() call causes issue in init.php

* fix old testcase name

* generic phpunit 7 fixes

* add missed file_get_contents
Keep migrations table content

* fix duplicate key

* Drop old php versions from travis-ci

* remove hhvm

* fix code climate message

* remove use of deprecated function assertInternalType

* Disable CSRF, we'll enable it separately.
All forms need to be updated to work.

* Update document references
2019-02-12 17:45:04 -06:00

143 lines
5.6 KiB
PHP

<?php
/**
* DBSetup.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Neil Lathwood
* @author Neil Lathwood <librenms+n@laf.io>
*/
namespace LibreNMS\Tests;
use \PHPUnit\Framework\ExpectationFailedException as PHPUnitException;
class DBSetupTest extends DBTestCase
{
public function testSetupDB()
{
global $schema;
foreach ($schema as $output) {
if (preg_match('/([1-9]+) errors/', $output) || preg_match('/Cannot execute query/', $output)) {
throw new PHPUnitException("Errors loading DB Schema: " . $output);
}
}
$this->expectNotToPerformAssertions();
}
public function testSchemaFiles()
{
global $config;
$files = glob($config['install_dir'].'/sql-schema/*.sql');
foreach ($files as $file) {
$content = file_get_contents($file);
foreach (explode("\n", $content) as $line) {
// skip comments and empty lines
if (empty($line) || starts_with($line, array('#', '--'))) {
continue;
}
// each line must end with ;, prevents multiline and makes sql easy to run by hand
// Warning may include whitespace such as space and \r
if (!ends_with($line, ';')) {
throw new PHPUnitException("Each line must end with a semicolin (;)\n$file: $line");
}
// cannot assume user use the librenms database name
if (str_contains($line, 'librenms.')) {
throw new PHPUnitException("Do not include the database name in schema files\n$file: $line");
}
}
}
$this->expectNotToPerformAssertions();
}
public function testSchema()
{
$files = array_map(function ($migration_file) {
return basename($migration_file, '.php');
}, array_diff(scandir(\LibreNMS\Config::get('install_dir') . '/database/migrations'), ['.', '..']));
$migrated = dbFetchColumn('SELECT migration FROM migrations');
sort($files);
sort($migrated);
$this->assertEquals($files, $migrated, "List of run migrations did not match existing migration files.");
$schema = get_db_schema();
$this->assertEquals(1000, $schema, "Seed not run, after seed legacy dbSchema should be 1000");
}
public function testCheckDBCollation()
{
$collation = dbFetchRows("SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA S WHERE schema_name = '$this->db_name' AND ( DEFAULT_CHARACTER_SET_NAME != 'utf8' OR DEFAULT_COLLATION_NAME != 'utf8_unicode_ci')");
if (isset($collation[0])) {
$error = implode(' ', $collation[0]);
} else {
$error = '';
}
$this->assertEmpty($collation, 'Wrong Database Collation or Character set: ' . $error);
}
public function testCheckTableCollation()
{
$collation = dbFetchRows("SELECT T.TABLE_NAME, C.CHARACTER_SET_NAME, C.COLLATION_NAME FROM information_schema.TABLES AS T, information_schema.COLLATION_CHARACTER_SET_APPLICABILITY AS C WHERE C.collation_name = T.table_collation AND T.table_schema = '$this->db_name' AND ( C.CHARACTER_SET_NAME != 'utf8' OR C.COLLATION_NAME != 'utf8_unicode_ci' );");
$error = '';
foreach ($collation as $id => $data) {
$error .= implode(' ', $data) . PHP_EOL;
}
$this->assertEmpty($collation, 'Wrong Table Collation or Character set: ' . $error);
}
public function testCheckColumnCollation()
{
$collation = dbFetchRows("SELECT TABLE_NAME, COLUMN_NAME, CHARACTER_SET_NAME, COLLATION_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '$this->db_name' AND ( CHARACTER_SET_NAME != 'utf8' OR COLLATION_NAME != 'utf8_unicode_ci' );");
$error = '';
foreach ($collation as $id => $data) {
$error .= implode(' ', $data) . PHP_EOL;
}
$this->assertEmpty($collation, 'Wrong Column Collation or Character set: ' . $error);
}
public function testSqlMode()
{
$this->assertEquals(
'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION',
dbFetchCell("SELECT @@sql_mode")
);
}
public function testValidateSchema()
{
if (is_file('misc/db_schema.yaml')) {
$master_schema = \Symfony\Component\Yaml\Yaml::parse(
file_get_contents('misc/db_schema.yaml')
);
$current_schema = dump_db_schema();
$message = "Schema does not match the excpected schema defined by misc/db_schema.yaml\n";
$message .= "If you have changed the schema, make sure you update it with ./scripts/build-schema.php\n";
$this->assertEquals($master_schema, $current_schema, $message);
}
}
}