Skip to content

Commit

Permalink
update to laravel 7 (#13)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Tom Schlick authored Apr 16, 2020
1 parent f7743a4 commit 9ac82a6
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
/phpunit.xml.dist export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.github export-ignore
40 changes: 40 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
17 changes: 14 additions & 3 deletions src/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -160,7 +163,7 @@ public function savePivotChanges(Collection $passing_changes, int $event_type, $

/**
* @param int $event_type
* @param $model
* @param Model $model
*
* @return array
*/
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/PostModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/TagModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9ac82a6

Please sign in to comment.