From 9ac82a6cb01b539c69a6ced0b33d90739e013916 Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Wed, 15 Apr 2020 22:35:26 -0400 Subject: [PATCH] update to laravel 7 (#13) * Update composer.json * Update .travis.yml * Update composer.json * import facade * use getRawOriginal() * make it clear what events are being caught by the default switch statment clause * manually specify the deleted_at change as laravel changed the behaviour on soft deletes * check if using soft deletes or not * travis config * replace travisci with github actions * update badge * revert to getOriginal() * revert * debug * fix * remove pull_request event from tests * export ignore for .github folder * exclude deleted type from original value --- .gitattributes | 1 + .github/workflows/tests.yml | 40 +++++++++++++++++++++++++++++++++++++ .travis.yml | 14 ------------- README.md | 2 +- composer.json | 10 +++++----- src/Models/BaseModel.php | 17 +++++++++++++--- tests/PostModelTest.php | 1 + tests/TagModelTest.php | 1 + 8 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/tests.yml delete mode 100644 .travis.yml diff --git a/.gitattributes b/.gitattributes index 531b44f..60a177e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -10,3 +10,4 @@ /phpunit.xml.dist export-ignore /tests export-ignore /.editorconfig export-ignore +/.github export-ignore diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..047102f --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,40 @@ +name: tests + +on: + push: + schedule: + - cron: '0 0 * * *' + +jobs: + tests: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + php: [7.2, 7.3, 7.4] + stability: [prefer-lowest, prefer-stable] + + name: PHP ${{ matrix.php }} - ${{ matrix.stability }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Cache dependencies + uses: actions/cache@v1 + with: + path: ~/.composer/cache/files + key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd + coverage: none + + - name: Install dependencies + run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction + + - name: Execute tests + run: vendor/bin/phpunit --testdox diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 721ae2a..0000000 --- a/.travis.yml +++ /dev/null @@ -1,14 +0,0 @@ -language: php - -php: - - 7.1 - - 7.2 - - 7.3 - - 7.4 - -before_script: - - travis_retry composer self-update - - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - -script: - - vendor/bin/phpunit diff --git a/README.md b/README.md index 8e9c2dd..06d5b6e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Laravel Model Auditlog [![Latest Version on Packagist](https://img.shields.io/packagist/v/orisintel/laravel-model-auditlog.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-model-auditlog) -[![Build Status](https://img.shields.io/travis/orisintel/laravel-model-auditlog/master.svg?style=flat-square)](https://travis-ci.org/orisintel/laravel-model-auditlog) +[![Build Status](https://img.shields.io/github/workflow/status/orisintel/laravel-model-auditlog/tests?style=flat-square)](https://github.com/orisintel/laravel-model-auditlog/actions?query=workflow%3Atests) [![Total Downloads](https://img.shields.io/packagist/dt/orisintel/laravel-model-auditlog.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-model-auditlog) When modifying a model record, it is nice to have a log of the changes made and who made those changes. There are many packages around this already, but this one is different in that it logs those changes to individual tables for performance and supports real foreign keys. diff --git a/composer.json b/composer.json index 511c9a1..56187e5 100644 --- a/composer.json +++ b/composer.json @@ -23,18 +23,18 @@ } ], "require": { - "php": "^7.1", + "php": "^7.2.5", "awobaz/compoships": "^1.1", "fico7489/laravel-pivot": "^3.0.1", - "laravel/framework": "^5.8 | ^6.0", - "orisintel/laravel-process-stamps": "^1.2" + "laravel/framework": "^7.0", + "orisintel/laravel-process-stamps": "^2.0" }, "require-dev": { "doctrine/dbal": "^2.9", "larapack/dd": "^1.0", "mockery/mockery": "~1.0", - "orchestra/testbench": "^3.8", - "phpunit/phpunit": "^7.0" + "orchestra/testbench": "^5.0", + "phpunit/phpunit": "^8.0|^9.0" }, "autoload": { "psr-4": { diff --git a/src/Models/BaseModel.php b/src/Models/BaseModel.php index d5f463f..5cc577c 100644 --- a/src/Models/BaseModel.php +++ b/src/Models/BaseModel.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Auth; use OrisIntel\AuditLog\EventType; /** @@ -69,11 +70,13 @@ public function saveChanges(Collection $passing_changes, int $event_type, $model } if (config('model-auditlog.enable_user_foreign_keys')) { - $log->user_id = \Auth::{config('model-auditlog.auth_id_function', 'id')}(); + $log->user_id = Auth::{config('model-auditlog.auth_id_function', 'id')}(); } $log->setAttribute('field_name', $key); - $log->setAttribute('field_value_old', $model->getOriginal($key)); + if($event_type !== EventType::DELETED and $model->getRawOriginal($key) !== $change) { + $log->setAttribute('field_value_old', $model->getRawOriginal($key)); + } $log->setAttribute('field_value_new', $change); $log->attributes; @@ -160,7 +163,7 @@ public function savePivotChanges(Collection $passing_changes, int $event_type, $ /** * @param int $event_type - * @param $model + * @param Model $model * * @return array */ @@ -176,6 +179,14 @@ public static function getChangesByType(int $event_type, $model) : array case EventType::FORCE_DELETED: return []; // if force deleted we want to stop execution here as there would be nothing to correlate records to break; + case EventType::DELETED: + if (method_exists($model, 'getDeletedAtColumn')) { + return $model->only($model->getDeletedAtColumn()); + } + + return []; + break; + case EventType::UPDATED: default: return $model->getDirty(); break; diff --git a/tests/PostModelTest.php b/tests/PostModelTest.php index dd3ab5c..594e74f 100644 --- a/tests/PostModelTest.php +++ b/tests/PostModelTest.php @@ -89,6 +89,7 @@ public function deleting_a_post_triggers_a_revision() $this->assertEquals(3, $post->auditLogs()->count()); $last = $post->auditLogs()->where('event_type', EventType::DELETED)->first(); + $this->assertEquals('deleted_at', $last->field_name); $this->assertNull($last->field_value_old); $this->assertNotEmpty($last->field_value_new); diff --git a/tests/TagModelTest.php b/tests/TagModelTest.php index 413a337..ec951a4 100644 --- a/tests/TagModelTest.php +++ b/tests/TagModelTest.php @@ -87,6 +87,7 @@ public function deleting_a_tag_triggers_a_revision() $this->assertEquals(3, $tag->auditLogs()->count()); $last = $tag->auditLogs()->where('event_type', EventType::DELETED)->first(); + $this->assertEquals('deleted_at', $last->field_name); $this->assertNull($last->field_value_old); $this->assertNotEmpty($last->field_value_new);