From 9d9aa71eb736885f23c069a75c7a047b06434178 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Thu, 30 Aug 2018 15:41:16 +0200 Subject: [PATCH 1/2] Installation: Added migrations for install.sql and update.sql files --- .gitlab-ci.yml | 13 ++-- README.md | 7 ++- bin/migrate | 19 +++++- .../2018_01_01_000001_import_install_sql.php | 59 +++++++++++++++++++ .../2018_01_01_000002_import_update_sql.php | 27 +++++++++ includes/autoload.php | 3 +- 6 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 db/migrations/2018_01_01_000001_import_install_sql.php create mode 100644 db/migrations/2018_01_01_000002_import_update_sql.php diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 250bf0db..8ae3d8ce 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,7 +2,7 @@ image: php cache: paths: - - .composer + - .composer services: - mariadb:10.2 @@ -29,15 +29,14 @@ before_script: - &before_install_xdebug |- pecl install xdebug docker-php-ext-enable xdebug - # MySQL DB - - &before_setup_mysql |- - apt install -yqq mariadb-client - mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/install.sql - mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/update.sql # Install Composer - &before_install_composer |- curl -sS https://getcomposer.org/installer | php -- --no-ansi --install-dir /usr/local/bin/ --filename composer /usr/local/bin/composer --no-ansi install + # MySQL DB + - &before_setup_mysql |- + chmod +x ./bin/migrate + ./bin/migrate .test_template: &test_definition stage: test @@ -61,7 +60,7 @@ test:7.1: services: [] stage: deploy only: - - master + - master before_script: - *before_fix_permissions - *before_install_packages diff --git a/README.md b/README.md index d56603a5..5bb0398a 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ To report bugs use [engelsystem/issues](https://github.com/engelsystem/engelsyst * Recommended: Directory Listing should be disabled. * There must a be MySQL database created with a user who has full rights to that database. - * It must be created by the db/install.sql and db/update.sql files. * If necessary, create a config/config.php to override values from config/config.default.php. + * To import the database the `bin/migrate` script has to be called. * In the browser, login with credentials admin:asdfasdf and change the password. Engelsystem can now be used. @@ -79,5 +79,10 @@ The `bin/deploy.sh` script can be used to deploy the engelsystem. It uses rsync For usage see `./bin/deploy.sh -h` +##### bin/migrate +The `bin/migrate` script can be used to import and update the database of the engelsystem. + +For more information on how to use it call `bin/migrate help` + ### Codestyle Please ensure that your pull requests follow [PSR-2](http://www.php-fig.org/psr/psr-2/) and [PSR-4](http://www.php-fig.org/psr/psr-4/). diff --git a/bin/migrate b/bin/migrate index 20ae2a93..ab3598d4 100755 --- a/bin/migrate +++ b/bin/migrate @@ -18,4 +18,21 @@ $app->register(MigrationServiceProvider::class); /** @var Migrate $migration */ $migration = $app->get('db.migration'); $migration->setOutput(function ($text) { echo $text . PHP_EOL; }); -$migration->run($baseDir, Migrate::UP); + +if (isset($argv[1]) && strtolower($argv[1]) == 'help') { + echo PHP_EOL . 'Usage: ' . $argv[1] . ' [up|down] [one-step]' . PHP_EOL . PHP_EOL; + exit; +} + +$method = Migrate::UP; +if (isset($argv[1]) && strtolower($argv[1]) == 'down') { + $argv = array_values($argv); + $method = Migrate::DOWN; +} + +$oneStep = false; +if (isset($argv[2]) && strtolower($argv[2]) == 'one-step') { + $oneStep = true; +} + +$migration->run($baseDir, $method, $oneStep); diff --git a/db/migrations/2018_01_01_000001_import_install_sql.php b/db/migrations/2018_01_01_000001_import_install_sql.php new file mode 100644 index 00000000..bd7ec7ae --- /dev/null +++ b/db/migrations/2018_01_01_000001_import_install_sql.php @@ -0,0 +1,59 @@ +oldTables as $table) { + if ($this->schema->hasTable($table)) { + return; + } + } + + $sql = file_get_contents(__DIR__ . '/../install.sql'); + $this->schema->getConnection()->unprepared($sql); + } + + + /** + * Reverse the migration + */ + public + function down() + { + $this->schema->getConnection()->statement('SET FOREIGN_KEY_CHECKS=0;'); + + foreach ($this->oldTables as $table) { + if ($this->schema->hasTable($table)) { + $this->schema->dropIfExists($table); + } + } + } +} diff --git a/db/migrations/2018_01_01_000002_import_update_sql.php b/db/migrations/2018_01_01_000002_import_update_sql.php new file mode 100644 index 00000000..58687d6c --- /dev/null +++ b/db/migrations/2018_01_01_000002_import_update_sql.php @@ -0,0 +1,27 @@ +schema->hasTable('UserWorkLog')) { + return; + } + + $sql = file_get_contents(__DIR__ . '/../update.sql'); + $this->schema->getConnection()->unprepared($sql); + } + + /** + * Reverse the migration + */ + public function down() + { + $this->schema->dropIfExists('UserWorkLog'); + } +} diff --git a/includes/autoload.php b/includes/autoload.php index 0cd9d355..f2e90b16 100644 --- a/includes/autoload.php +++ b/includes/autoload.php @@ -2,7 +2,8 @@ // Check for autoloader if (!is_readable(__DIR__ . '/../vendor/autoload.php')) { - die('Please run composer.phar install'); + echo 'Please run composer.phar install'; + exit(1); } // Include composer autoloader From ac48332166ce28fcb1a2fc130c7f5adbc760e42d Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Fri, 31 Aug 2018 01:55:05 +0200 Subject: [PATCH 2/2] Models: Added LogEntry model --- ..._08_30_000000_create_log_entries_table.php | 47 ++++++++++++++ includes/includes.php | 1 - includes/model/LogEntries_model.php | 62 ------------------- includes/pages/admin_log.php | 20 +++--- src/Logger/EngelsystemLogger.php | 13 +++- src/Models/BaseModel.php | 23 +++++++ src/Models/LogEntry.php | 46 ++++++++++++++ .../Feature/Logger/EngelsystemLoggerTest.php | 21 ++++--- tests/Feature/Model/LogEntriesModelTest.php | 33 ---------- tests/Feature/Model/LogEntryTest.php | 56 +++++++++++++++++ .../Unit/Logger/LoggerServiceProviderTest.php | 1 + tests/Unit/Models/BaseModelTest.php | 22 +++++++ .../Models/Stub/BaseModelImplementation.php | 27 ++++++++ 13 files changed, 257 insertions(+), 115 deletions(-) create mode 100644 db/migrations/2018_08_30_000000_create_log_entries_table.php delete mode 100644 includes/model/LogEntries_model.php create mode 100644 src/Models/BaseModel.php create mode 100644 src/Models/LogEntry.php delete mode 100644 tests/Feature/Model/LogEntriesModelTest.php create mode 100644 tests/Feature/Model/LogEntryTest.php create mode 100644 tests/Unit/Models/BaseModelTest.php create mode 100644 tests/Unit/Models/Stub/BaseModelImplementation.php diff --git a/db/migrations/2018_08_30_000000_create_log_entries_table.php b/db/migrations/2018_08_30_000000_create_log_entries_table.php new file mode 100644 index 00000000..68815434 --- /dev/null +++ b/db/migrations/2018_08_30_000000_create_log_entries_table.php @@ -0,0 +1,47 @@ +schema->create('log_entries', function (Blueprint $table) { + $table->increments('id'); + $table->string('level', 20); + $table->text('message'); + $table->timestamp('created_at')->nullable(); + }); + + $this->schema->getConnection()->unprepared(' + INSERT INTO log_entries (`id`, `level`, `message`, `created_at`) + SELECT `id`, `level`, `message`, FROM_UNIXTIME(`timestamp`) FROM LogEntries + '); + + $this->schema->dropIfExists('LogEntries'); + } + + /** + * Reverse the migration + */ + public function down() + { + $this->schema->create('LogEntries', function (Blueprint $table) { + $table->increments('id'); + $table->string('level', 20); + $table->text('message'); + $table->integer('timestamp'); + }); + + $this->schema->getConnection()->unprepared(' + INSERT INTO LogEntries (`id`, `level`, `message`, `timestamp`) + SELECT `id`, `level`, `message`, UNIX_TIMESTAMP(`created_at`) FROM log_entries + '); + + $this->schema->dropIfExists('log_entries'); + } +} diff --git a/includes/includes.php b/includes/includes.php index 7a68c3c9..628cf88e 100644 --- a/includes/includes.php +++ b/includes/includes.php @@ -13,7 +13,6 @@ $includeFiles = [ __DIR__ . '/../includes/model/AngelType_model.php', __DIR__ . '/../includes/model/EventConfig_model.php', - __DIR__ . '/../includes/model/LogEntries_model.php', __DIR__ . '/../includes/model/Message_model.php', __DIR__ . '/../includes/model/NeededAngelTypes_model.php', __DIR__ . '/../includes/model/Room_model.php', diff --git a/includes/model/LogEntries_model.php b/includes/model/LogEntries_model.php deleted file mode 100644 index b16c598c..00000000 --- a/includes/model/LogEntries_model.php +++ /dev/null @@ -1,62 +0,0 @@ -statement('TRUNCATE `LogEntries`'); -} diff --git a/includes/pages/admin_log.php b/includes/pages/admin_log.php index f80f59d2..2a736aa5 100644 --- a/includes/pages/admin_log.php +++ b/includes/pages/admin_log.php @@ -1,5 +1,7 @@ has('keyword')) { $filter = strip_request_item('keyword'); } - $log_entries = LogEntries_filter($filter); - foreach ($log_entries as &$log_entry) { - $log_entry['date'] = date('d.m.Y H:i', $log_entry['timestamp']); + $log_entries = LogEntry::filter($filter); + + $entries = []; + foreach ($log_entries as $entry) { + $data = $entry->toArray(); + $data['created_at'] = date_format($entry->created_at, 'd.m.Y H:i'); + $entries[] = $data; } return page_with_title(admin_log_title(), [ @@ -30,9 +36,9 @@ function admin_log() form_submit(__('Search'), 'Go') ]), table([ - 'date' => 'Time', - 'level' => 'Type', - 'message' => 'Log Entry' - ], $log_entries) + 'created_at' => 'Time', + 'level' => 'Type', + 'message' => 'Log Entry' + ], $entries) ]); } diff --git a/src/Logger/EngelsystemLogger.php b/src/Logger/EngelsystemLogger.php index 1f255b69..aab8026a 100644 --- a/src/Logger/EngelsystemLogger.php +++ b/src/Logger/EngelsystemLogger.php @@ -2,6 +2,7 @@ namespace Engelsystem\Logger; +use Engelsystem\Models\LogEntry; use Psr\Log\AbstractLogger; use Psr\Log\InvalidArgumentException; use Psr\Log\LogLevel; @@ -19,6 +20,14 @@ class EngelsystemLogger extends AbstractLogger LogLevel::WARNING, ]; + /** @var LogEntry */ + protected $log; + + public function __construct(LogEntry $log) + { + $this->log = $log; + } + /** * Logs with an arbitrary level. * @@ -33,12 +42,12 @@ class EngelsystemLogger extends AbstractLogger public function log($level, $message, array $context = []) { if (!$this->checkLevel($level)) { - throw new InvalidArgumentException(); + throw new InvalidArgumentException('Unknown log level: ' . $level); } $message = $this->interpolate($message, $context); - LogEntry_create($level, $message); + $this->log->create(['level' => $level, 'message' => $message]); } /** diff --git a/src/Models/BaseModel.php b/src/Models/BaseModel.php new file mode 100644 index 00000000..cf718e4f --- /dev/null +++ b/src/Models/BaseModel.php @@ -0,0 +1,23 @@ +save(); + + return $instance; + } +} diff --git a/src/Models/LogEntry.php b/src/Models/LogEntry.php new file mode 100644 index 00000000..ca9702de --- /dev/null +++ b/src/Models/LogEntry.php @@ -0,0 +1,46 @@ +select() + ->orderByDesc('created_at') + ->orderByDesc('id') + ->limit(10000); + + if (!empty($keyword)) { + $query + ->where('level', '=', $keyword) + ->orWhere('message', 'LIKE', '%' . $keyword . '%'); + } + + return $query->get(); + } +} diff --git a/tests/Feature/Logger/EngelsystemLoggerTest.php b/tests/Feature/Logger/EngelsystemLoggerTest.php index ce7b8ac5..9ec078a3 100644 --- a/tests/Feature/Logger/EngelsystemLoggerTest.php +++ b/tests/Feature/Logger/EngelsystemLoggerTest.php @@ -3,6 +3,7 @@ namespace Engelsystem\Test\Feature\Logger; use Engelsystem\Logger\EngelsystemLogger; +use Engelsystem\Models\LogEntry; use Engelsystem\Test\Feature\ApplicationFeatureTest; use Psr\Log\InvalidArgumentException; use Psr\Log\LoggerInterface; @@ -15,12 +16,13 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest */ public function getLogger() { - return new EngelsystemLogger(); + $logEntry = new LogEntry(); + return new EngelsystemLogger($logEntry); } public function testImplements() { - $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); + $this->assertInstanceOf(LoggerInterface::class, $this->getLogger()); } /** @@ -46,21 +48,20 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest */ public function testAllLevels($level) { + LogEntry::query()->truncate(); $logger = $this->getLogger(); - LogEntries_clear_all(); - $logger->log($level, 'First log message'); $logger->{$level}('Second log message'); - $entries = LogEntries(); + $entries = LogEntry::all(); $this->assertCount(2, $entries); } public function testContextReplacement() { + LogEntry::query()->truncate(); $logger = $this->getLogger(); - LogEntries_clear_all(); $logger->log(LogLevel::INFO, 'My username is {username}', ['username' => 'Foo']); @@ -100,8 +101,8 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest public function testContextToString() { + LogEntry::query()->truncate(); $logger = $this->getLogger(); - LogEntries_clear_all(); $mock = $this->getMockBuilder('someDataProvider') ->setMethods(['__toString']) @@ -132,14 +133,14 @@ class EngelsystemLoggerTest extends ApplicationFeatureTest */ public function getLastEntry() { - $entries = LogEntries(); - $entry = array_pop($entries); + $entries = LogEntry::all(); + $entry = $entries->last(); return $entry; } public function tearDown() { - LogEntries_clear_all(); + LogEntry::query()->truncate(); } } diff --git a/tests/Feature/Model/LogEntriesModelTest.php b/tests/Feature/Model/LogEntriesModelTest.php deleted file mode 100644 index c032a94c..00000000 --- a/tests/Feature/Model/LogEntriesModelTest.php +++ /dev/null @@ -1,33 +0,0 @@ -assertNotFalse(LogEntry_create(LogLevel::WARNING, 'test_LogEntry_create')); - - // There should be one more log entry now - $this->assertEquals(count(LogEntries()), $count + 1); - } - - public function testClearAllLogEntries() - { - LogEntry_create(LogLevel::WARNING, 'test'); - $this->assertTrue(count(LogEntries()) > 0); - - $this->assertNotFalse(LogEntries_clear_all()); - $this->assertCount(0, LogEntries()); - } - - public function tearDown() - { - LogEntries_clear_all(); - } -} diff --git a/tests/Feature/Model/LogEntryTest.php b/tests/Feature/Model/LogEntryTest.php new file mode 100644 index 00000000..25b35676 --- /dev/null +++ b/tests/Feature/Model/LogEntryTest.php @@ -0,0 +1,56 @@ + LogLevel::INFO, + 'Some test content' => LogLevel::ERROR, + 'Foo bar bartz!' => LogLevel::INFO, + 'Someone did something?' => LogLevel::NOTICE, + 'This is a Test!' => LogLevel::INFO, + 'I\'m verbose notice!' => LogLevel::DEBUG, + 'The newest stuff!!' => LogLevel::ERROR, + ] as $message => $level) { + $entry = new LogEntry(['level' => $level, 'message' => $message]); + $entry->save(); + } + + $model = new LogEntry(); + + $return = $model->filter(); + $this->assertCount(7, $return); + + /** @var LogEntry $first */ + $first = $return->first(); + + $this->assertEquals('The newest stuff!!', $first->message); + + $return = $model->filter(LogLevel::INFO); + $this->assertCount(3, $return); + + $return = $model->filter('notice'); + $this->assertCount(2, $return); + + $return = $model->filter('bartz'); + $this->assertCount(1, $return); + } + + /** + * This method is called before a test is executed. + */ + public function setUp() + { + LogEntry::query()->truncate(); + } +} diff --git a/tests/Unit/Logger/LoggerServiceProviderTest.php b/tests/Unit/Logger/LoggerServiceProviderTest.php index cef95d5b..aef0b7d1 100644 --- a/tests/Unit/Logger/LoggerServiceProviderTest.php +++ b/tests/Unit/Logger/LoggerServiceProviderTest.php @@ -17,6 +17,7 @@ class LoggerServiceProviderTest extends ServiceProviderTest { /** @var PHPUnit_Framework_MockObject_MockObject|EngelsystemLogger $logger */ $logger = $this->getMockBuilder(EngelsystemLogger::class) + ->disableOriginalConstructor() ->getMock(); $app = $this->getApp(['make', 'instance', 'bind']); diff --git a/tests/Unit/Models/BaseModelTest.php b/tests/Unit/Models/BaseModelTest.php new file mode 100644 index 00000000..52cb8c7b --- /dev/null +++ b/tests/Unit/Models/BaseModelTest.php @@ -0,0 +1,22 @@ +create(['foo' => 'bar']); + + $this->assertNotEquals($model, $newModel); + $this->assertEquals('bar', $newModel->foo); + $this->assertEquals(1, $newModel->saveCount); + } +} diff --git a/tests/Unit/Models/Stub/BaseModelImplementation.php b/tests/Unit/Models/Stub/BaseModelImplementation.php new file mode 100644 index 00000000..4aa1ef0b --- /dev/null +++ b/tests/Unit/Models/Stub/BaseModelImplementation.php @@ -0,0 +1,27 @@ +saveCount++; + return true; + } +}