Skip to content

Commit ef9b1eb

Browse files
authored
Merge pull request #50 from UseMuffin/slug-on-update
Fix slug updating.
2 parents 2d3366a + 54c3f17 commit ef9b1eb

4 files changed

Lines changed: 36 additions & 20 deletions

File tree

.github/workflows/ci.yml

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ on: [push, pull_request]
44

55
jobs:
66
testsuite:
7-
runs-on: ubuntu-18.04
7+
runs-on: ubuntu-22.04
88
strategy:
99
fail-fast: false
1010
matrix:
11-
php-version: ['7.2', '8.0', '8.1']
11+
php-version: ['7.4', '8.0', '8.1', '8.2']
1212
db-type: [sqlite, mysql, pgsql]
1313
prefer-lowest: ['']
1414
include:
@@ -25,9 +25,7 @@ jobs:
2525
POSTGRES_PASSWORD: postgres
2626

2727
steps:
28-
- uses: actions/checkout@v2
29-
with:
30-
fetch-depth: 1
28+
- uses: actions/checkout@v3
3129

3230
- name: Setup PHP
3331
uses: shivammathur/setup-php@v2
@@ -50,26 +48,24 @@ jobs:
5048
if [[ ${{ matrix.db-type }} == 'mysql' ]]; then export DB_DSN='mysql://root:root@127.0.0.1/cakephp'; fi
5149
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then export DB_DSN='postgres://postgres:postgres@127.0.0.1/postgres'; fi
5250
53-
if [[ ${{ matrix.php-version }} == '7.2' && ${{ matrix.db-type }} == 'sqlite' ]]; then
51+
if [[ ${{ matrix.php-version }} == '7.4' && ${{ matrix.db-type }} == 'sqlite' ]]; then
5452
vendor/bin/phpunit --coverage-clover=coverage.xml
5553
else
5654
vendor/bin/phpunit
5755
fi
5856
5957
- name: Code Coverage Report
60-
if: matrix.php-version == '7.2' && matrix.db-type == 'sqlite'
58+
if: matrix.php-version == '7.4' && matrix.db-type == 'sqlite'
6159
env:
6260
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
6361
run: bash <(curl -s https://codecov.io/bash)
6462

6563
coding-standard:
6664
name: Coding Standard
67-
runs-on: ubuntu-18.04
65+
runs-on: ubuntu-22.04
6866

6967
steps:
70-
- uses: actions/checkout@v2
71-
with:
72-
fetch-depth: 1
68+
- uses: actions/checkout@v3
7369

7470
- name: Setup PHP
7571
uses: shivammathur/setup-php@v2
@@ -86,19 +82,17 @@ jobs:
8682

8783
static-analysis:
8884
name: Static Analysis
89-
runs-on: ubuntu-18.04
85+
runs-on: ubuntu-22.04
9086

9187
steps:
92-
- uses: actions/checkout@v2
93-
with:
94-
fetch-depth: 1
88+
- uses: actions/checkout@v3
9589

9690
- name: Setup PHP
9791
uses: shivammathur/setup-php@v2
9892
with:
9993
php-version: '8.0'
100-
extension-csv: mbstring, intl
101-
coverage: none,
94+
extensions: mbstring, intl
95+
coverage: none
10296
tools: vimeo/psalm:4.1
10397

10498
- name: Composer Install

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,11 @@
4747
"psr-4": {
4848
"Muffin\\Slug\\Test\\": "tests"
4949
}
50+
},
51+
"config": {
52+
"sort-packages": true,
53+
"allow-plugins": {
54+
"dealerdirect/phpcodesniffer-composer-installer": true
55+
}
5056
}
5157
}

src/Model/Behavior/SlugBehavior.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,24 @@ public function beforeSave(EventInterface $event, EntityInterface $entity, Array
232232

233233
$onDirty = $this->getConfig('onDirty');
234234
$field = $this->getConfig('field');
235-
if ($onDirty && !$entity->isDirty($field) && !$isNew) {
236-
return;
235+
$slugFieldDirty = $entity->isDirty($field);
236+
237+
if ($onDirty && !$slugFieldDirty && !$isNew) {
238+
$displayFieldDirty = false;
239+
foreach ((array)$this->getConfig('displayField') as $df) {
240+
if ($entity->isDirty($df)) {
241+
$displayFieldDirty = true;
242+
break;
243+
}
244+
}
245+
246+
if (!$displayFieldDirty) {
247+
return;
248+
}
237249
}
238250

239251
$separator = $this->getConfig('separator');
240-
if ($entity->isDirty($field) && !empty($entity->{$field})) {
252+
if ($slugFieldDirty && !empty($entity->{$field})) {
241253
$slug = $this->slug($entity, $entity->{$field}, $separator);
242254
$entity->set($field, $slug);
243255

tests/TestCase/Model/Behavior/SlugBehaviorTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ public function testBeforeSaveOnUpdateTrueAndOnDirtyTrue()
172172
$tag = $this->Tags->save($tag);
173173
$this->assertEquals('bar', $tag->slug);
174174
$this->assertSame(2, $tag->counter);
175+
176+
$tag->name = 'changed';
177+
$tag = $this->Tags->save($tag);
178+
$this->assertEquals('changed', $tag->slug);
175179
}
176180

177181
/**

0 commit comments

Comments
 (0)