Skip to content

Commit 9ac82a6

Browse files
author
Tom Schlick
authored
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
1 parent f7743a4 commit 9ac82a6

File tree

8 files changed

+63
-23
lines changed

8 files changed

+63
-23
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
/phpunit.xml.dist export-ignore
1111
/tests export-ignore
1212
/.editorconfig export-ignore
13+
/.github export-ignore

.github/workflows/tests.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
schedule:
6+
- cron: '0 0 * * *'
7+
8+
jobs:
9+
tests:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: true
13+
matrix:
14+
php: [7.2, 7.3, 7.4]
15+
stability: [prefer-lowest, prefer-stable]
16+
17+
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v2
22+
23+
- name: Cache dependencies
24+
uses: actions/cache@v1
25+
with:
26+
path: ~/.composer/cache/files
27+
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
28+
29+
- name: Setup PHP
30+
uses: shivammathur/setup-php@v2
31+
with:
32+
php-version: ${{ matrix.php }}
33+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd
34+
coverage: none
35+
36+
- name: Install dependencies
37+
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction
38+
39+
- name: Execute tests
40+
run: vendor/bin/phpunit --testdox

.travis.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Laravel Model Auditlog
22

33
[![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)
4-
[![Build Status](https://img.shields.io/travis/orisintel/laravel-model-auditlog/master.svg?style=flat-square)](https://travis-ci.org/orisintel/laravel-model-auditlog)
4+
[![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)
55
[![Total Downloads](https://img.shields.io/packagist/dt/orisintel/laravel-model-auditlog.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-model-auditlog)
66

77
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.

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
}
2424
],
2525
"require": {
26-
"php": "^7.1",
26+
"php": "^7.2.5",
2727
"awobaz/compoships": "^1.1",
2828
"fico7489/laravel-pivot": "^3.0.1",
29-
"laravel/framework": "^5.8 | ^6.0",
30-
"orisintel/laravel-process-stamps": "^1.2"
29+
"laravel/framework": "^7.0",
30+
"orisintel/laravel-process-stamps": "^2.0"
3131
},
3232
"require-dev": {
3333
"doctrine/dbal": "^2.9",
3434
"larapack/dd": "^1.0",
3535
"mockery/mockery": "~1.0",
36-
"orchestra/testbench": "^3.8",
37-
"phpunit/phpunit": "^7.0"
36+
"orchestra/testbench": "^5.0",
37+
"phpunit/phpunit": "^8.0|^9.0"
3838
},
3939
"autoload": {
4040
"psr-4": {

src/Models/BaseModel.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\Relations\BelongsTo;
77
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Facades\Auth;
89
use OrisIntel\AuditLog\EventType;
910

1011
/**
@@ -69,11 +70,13 @@ public function saveChanges(Collection $passing_changes, int $event_type, $model
6970
}
7071

7172
if (config('model-auditlog.enable_user_foreign_keys')) {
72-
$log->user_id = \Auth::{config('model-auditlog.auth_id_function', 'id')}();
73+
$log->user_id = Auth::{config('model-auditlog.auth_id_function', 'id')}();
7374
}
7475

7576
$log->setAttribute('field_name', $key);
76-
$log->setAttribute('field_value_old', $model->getOriginal($key));
77+
if($event_type !== EventType::DELETED and $model->getRawOriginal($key) !== $change) {
78+
$log->setAttribute('field_value_old', $model->getRawOriginal($key));
79+
}
7780
$log->setAttribute('field_value_new', $change);
7881

7982
$log->attributes;
@@ -160,7 +163,7 @@ public function savePivotChanges(Collection $passing_changes, int $event_type, $
160163

161164
/**
162165
* @param int $event_type
163-
* @param $model
166+
* @param Model $model
164167
*
165168
* @return array
166169
*/
@@ -176,6 +179,14 @@ public static function getChangesByType(int $event_type, $model) : array
176179
case EventType::FORCE_DELETED:
177180
return []; // if force deleted we want to stop execution here as there would be nothing to correlate records to
178181
break;
182+
case EventType::DELETED:
183+
if (method_exists($model, 'getDeletedAtColumn')) {
184+
return $model->only($model->getDeletedAtColumn());
185+
}
186+
187+
return [];
188+
break;
189+
case EventType::UPDATED:
179190
default:
180191
return $model->getDirty();
181192
break;

tests/PostModelTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function deleting_a_post_triggers_a_revision()
8989
$this->assertEquals(3, $post->auditLogs()->count());
9090

9191
$last = $post->auditLogs()->where('event_type', EventType::DELETED)->first();
92+
9293
$this->assertEquals('deleted_at', $last->field_name);
9394
$this->assertNull($last->field_value_old);
9495
$this->assertNotEmpty($last->field_value_new);

tests/TagModelTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function deleting_a_tag_triggers_a_revision()
8787
$this->assertEquals(3, $tag->auditLogs()->count());
8888

8989
$last = $tag->auditLogs()->where('event_type', EventType::DELETED)->first();
90+
9091
$this->assertEquals('deleted_at', $last->field_name);
9192
$this->assertNull($last->field_value_old);
9293
$this->assertNotEmpty($last->field_value_new);

0 commit comments

Comments
 (0)