Skip to content

Commit

Permalink
Merge branch 'feature/87-auto-trim' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
polosson committed Jan 13, 2021
2 parents 0b4cb86 + 873ae02 commit 32fa00f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Ce projet adhère au principe du [Semantic Versioning](https://semver.org/spec/v
- Utilise des onglets dans la page de vue du matériel.
- Dans l'édition d'événements, la recherche directe des bénéficiaires et techniciens dans le champ multiple permet de tous les retrouver (#36).
- Ajoute des boutons dans la page des catégories, permettant d'ouvrir la liste du matériel d'une catégorie ou sous-catégorie (#51).
- Supprime automatiquement les espaces vides inutiles dans les champs des formulaires (#87).

## 0.10.2 (2020-11-16)

Expand Down
12 changes: 12 additions & 0 deletions server/src/App/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public function edit(?int $id = null, array $data = []): Model
}

$data = cleanEmptyFields($data);
$data = $this->_trimStringFields($data);

try {
$model = self::firstOrNew(compact('id'));
$model->fill($data)->validate()->save();
Expand Down Expand Up @@ -284,4 +286,14 @@ protected function _setSearchConditions(Builder $builder): Builder

return $builder->where($this->searchField, 'LIKE', $term);
}

protected function _trimStringFields(array $data): array
{
$trimmedData = [];
foreach ($data as $field => $value) {
$isString = array_key_exists($field, $this->casts) && $this->casts[$field] === 'string';
$trimmedData[$field] = ($isString && $value) ? trim($value) : $value;
}
return $trimmedData;
}
}
1 change: 1 addition & 0 deletions server/src/App/Models/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public function edit(?int $id = null, array $data = []): Model
}

$data = cleanEmptyFields($data);
$data = $this->_trimStringFields($data);

if (!empty($data['phone'])) {
$data['phone'] = normalizePhone($data['phone']);
Expand Down
4 changes: 2 additions & 2 deletions server/tests/models/CompanyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ public function testCreateCompanyDuplicate(): void
public function testCreateCompany(): void
{
$data = [
'legal_name' => 'test company',
'legal_name' => ' test company ',
'street' => 'Somewhere street, 123',
'postal_code' => '75000',
'locality' => 'Paris',
'country_id' => 1,
'phone' => '+00336 25 25 21 25',
];
$result = $this->model->edit(null, $data);
$result = $this->model->edit(null, $data);
$expected = [
'id' => 3,
'legal_name' => 'test company',
Expand Down
53 changes: 52 additions & 1 deletion server/tests/models/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testGetAllFilteredOrTagged(): void
$this->assertEmpty($result);
}

public function testGetUser(): void
public function testGetPerson(): void
{
$Person = $this->model::find(1);
$this->assertEquals([
Expand Down Expand Up @@ -235,4 +235,55 @@ public function testCreatePerson(): void
'phone' => 'notAphoneNumber',
]);
}

public function testUpdatePerson(): void
{
$result = $this->model->edit(1, [
'first_name' => ' Jeannot ',
'nickname' => ' testMan ',
]);
$expected = [
'id' => 1,
'user_id' => 1,
'first_name' => 'Jeannot',
'last_name' => 'Fountain',
'full_name' => 'Jeannot Fountain',
'nickname' => 'testMan',
'email' => 'tester@robertmanager.net',
'phone' => null,
'street' => '1, somewhere av.',
'postal_code' => '1234',
'locality' => 'Megacity',
'country_id' => 1,
'company_id' => 1,
'note' => null,
'company' => [
'id' => 1,
'legal_name' => 'Testing, Inc',
'street' => '1, company st.',
'postal_code' => '1234',
'locality' => 'Megacity',
'country_id' => 1,
'phone' => '+4123456789',
'note' => 'Just for tests',
'created_at' => null,
'updated_at' => null,
'deleted_at' => null,
'country' => [
'id' => 1,
'name' => 'France',
'code' => 'FR',
],
],
'country' => [
'id' => 1,
'name' => 'France',
'code' => 'FR',
],
];
unset($result->created_at);
unset($result->updated_at);
unset($result->deleted_at);
$this->assertEquals($expected, $result->toArray());
}
}

0 comments on commit 32fa00f

Please sign in to comment.