Skip to content

Commit 37e7be5

Browse files
author
Carlos Garcia
committed
Actualizada la clase PageFilter y PageOption para utilizar las clases y traits correctos, se ha especificado el tipo de retorno en los métodos clear y loadFromData, y se ha añadido el método save para manejar la codificación de valores. Se ha creado la clase de pruebas unitarias PageFilterTest para verificar la funcionalidad de PageFilter.
1 parent a71098d commit 37e7be5

File tree

5 files changed

+110
-40
lines changed

5 files changed

+110
-40
lines changed

Core/Model/PageFilter.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2017-2023 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2017-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -19,6 +19,8 @@
1919

2020
namespace FacturaScripts\Core\Model;
2121

22+
use FacturaScripts\Core\Template\ModelClass;
23+
use FacturaScripts\Core\Template\ModelTrait;
2224
use FacturaScripts\Core\Tools;
2325

2426
/**
@@ -27,9 +29,9 @@
2729
*
2830
* @author Artex Trading sa <jcuello@artextrading.com>
2931
*/
30-
class PageFilter extends Base\ModelClass
32+
class PageFilter extends ModelClass
3133
{
32-
use Base\ModelTrait;
34+
use ModelTrait;
3335

3436
/**
3537
* Human description
@@ -66,7 +68,7 @@ class PageFilter extends Base\ModelClass
6668
*/
6769
public $nick;
6870

69-
public function clear()
71+
public function clear(): void
7072
{
7173
parent::clear();
7274
$this->filters = [];
@@ -86,17 +88,25 @@ public function install(): string
8688
* @param array $data
8789
* @param array $exclude
8890
*/
89-
public function loadFromData(array $data = [], array $exclude = [])
91+
public function loadFromData(array $data = [], array $exclude = []): void
9092
{
9193
array_push($exclude, 'filters', 'code', 'action');
9294
parent::loadFromData($data, $exclude);
9395

9496
$this->filters = isset($data['filters']) ? json_decode($data['filters'], true) : [];
9597
}
9698

97-
public static function primaryColumn(): string
99+
public function save(): bool
98100
{
99-
return 'id';
101+
// Encode the filters values to JSON format
102+
$this->filters = $this->getEncodeValues()['filters'];
103+
104+
$saved = parent::save();
105+
106+
// decode the filters values back to array format
107+
$this->filters = json_decode($this->filters, true);
108+
109+
return $saved;
100110
}
101111

102112
public static function tableName(): string
@@ -126,14 +136,4 @@ private function getEncodeValues(): array
126136
'filters' => json_encode($this->filters)
127137
];
128138
}
129-
130-
protected function saveInsert(array $values = []): bool
131-
{
132-
return parent::saveInsert($this->getEncodeValues());
133-
}
134-
135-
protected function saveUpdate(array $values = []): bool
136-
{
137-
return parent::saveUpdate($this->getEncodeValues());
138-
}
139139
}

Core/Model/PageOption.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2017-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2017-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -19,6 +19,8 @@
1919

2020
namespace FacturaScripts\Core\Model;
2121

22+
use FacturaScripts\Core\Template\ModelClass;
23+
use FacturaScripts\Core\Template\ModelTrait;
2224
use FacturaScripts\Core\Tools;
2325

2426
/**
@@ -28,9 +30,9 @@
2830
* @author Jose Antonio Cuello <yopli2000@gmail.com>
2931
* @author Carlos García Gómez <carlos@facturascripts.com>
3032
*/
31-
class PageOption extends Base\ModelClass
33+
class PageOption extends ModelClass
3234
{
33-
use Base\ModelTrait;
35+
use ModelTrait;
3436

3537
/**
3638
* Definition of the columns. It is called columns but it always
@@ -82,7 +84,7 @@ class PageOption extends Base\ModelClass
8284
*/
8385
public $rows;
8486

85-
public function clear()
87+
public function clear(): void
8688
{
8789
parent::clear();
8890
$this->columns = [];
@@ -105,7 +107,7 @@ public function install(): string
105107
* @param array $data
106108
* @param array $exclude
107109
*/
108-
public function loadFromData(array $data = [], array $exclude = [])
110+
public function loadFromData(array $data = [], array $exclude = []): void
109111
{
110112
array_push($exclude, 'columns', 'modals', 'filters', 'rows', 'code', 'action');
111113
parent::loadFromData($data, $exclude);
@@ -115,9 +117,21 @@ public function loadFromData(array $data = [], array $exclude = [])
115117
$this->rows = json_decode($data['rows'], true);
116118
}
117119

118-
public static function primaryColumn(): string
120+
public function save(): bool
119121
{
120-
return 'id';
122+
// encode the values of the view configuration fields
123+
$this->columns = $this->getEncodeValues()['columns'];
124+
$this->modals = $this->getEncodeValues()['modals'];
125+
$this->rows = $this->getEncodeValues()['rows'];
126+
127+
$saved = parent::save();
128+
129+
// decode the values of the view configuration fields
130+
$this->columns = json_decode($this->columns, true);
131+
$this->modals = json_decode($this->modals, true);
132+
$this->rows = json_decode($this->rows, true);
133+
134+
return $saved;
121135
}
122136

123137
public static function tableName(): string
@@ -145,18 +159,4 @@ private function getEncodeValues(): array
145159
'rows' => json_encode($this->rows),
146160
];
147161
}
148-
149-
protected function saveInsert(array $values = []): bool
150-
{
151-
$this->last_update = Tools::dateTime();
152-
153-
return parent::saveInsert($this->getEncodeValues());
154-
}
155-
156-
protected function saveUpdate(array $values = []): bool
157-
{
158-
$this->last_update = Tools::dateTime();
159-
160-
return parent::saveUpdate($this->getEncodeValues());
161-
}
162162
}

Test/Core/Model/PageFilterTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* This file is part of FacturaScripts
4+
* Copyright (C) 2017-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
namespace FacturaScripts\Test\Core\Model;
21+
22+
use FacturaScripts\Core\Model\PageFilter;
23+
use FacturaScripts\Test\Traits\LogErrorsTrait;
24+
use FacturaScripts\Test\Traits\RandomDataTrait;
25+
use PHPUnit\Framework\TestCase;
26+
27+
final class PageFilterTest extends TestCase
28+
{
29+
use LogErrorsTrait;
30+
use RandomDataTrait;
31+
32+
public function testCreate(): void
33+
{
34+
// creamos un usuario
35+
$user = $this->getRandomUser();
36+
$this->assertTrue($user->save());
37+
38+
// creamos un filtro de página
39+
$pageFilter = new PageFilter();
40+
$pageFilter->name = 'TestController';
41+
$pageFilter->nick = $user->nick;
42+
$pageFilter->description = 'Test Filter';
43+
$pageFilter->filters = ['field1' => 'value1', 'field2' => 'value2'];
44+
$this->assertTrue($pageFilter->save(), 'Error saving PageFilter');
45+
46+
// comprobamos que existe en la base de datos
47+
$this->assertTrue($pageFilter->exists());
48+
49+
// comprobamos que se ha asignado un id
50+
$this->assertNotNull($pageFilter->id);
51+
52+
// eliminamos
53+
$this->assertTrue($pageFilter->delete(), 'Error deleting PageFilter');
54+
$this->assertTrue($user->delete());
55+
}
56+
57+
protected function tearDown(): void
58+
{
59+
$this->logErrors();
60+
}
61+
}

Test/Core/Model/PageOptionTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2024 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2024-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -20,10 +20,13 @@
2020
namespace FacturaScripts\Test\Core\Model;
2121

2222
use FacturaScripts\Core\Model\PageOption;
23+
use FacturaScripts\Test\Traits\LogErrorsTrait;
2324
use PHPUnit\Framework\TestCase;
2425

2526
final class PageOptionTest extends TestCase
2627
{
28+
use LogErrorsTrait;
29+
2730
public function testCreate(): void
2831
{
2932
// creamos
@@ -34,4 +37,9 @@ public function testCreate(): void
3437
// eliminamos
3538
$this->assertTrue($pageOption->delete(), 'Error deleting PageOption');
3639
}
40+
41+
protected function tearDown(): void
42+
{
43+
$this->logErrors();
44+
}
3745
}

Test/Traits/RandomDataTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace FacturaScripts\Test\Traits;
2121

22+
use FacturaScripts\Core\Tools;
2223
use FacturaScripts\Dinamic\Lib\Calculator;
2324
use FacturaScripts\Dinamic\Model\Agente;
2425
use FacturaScripts\Dinamic\Model\Almacen;
@@ -230,7 +231,7 @@ protected function getRandomUser(): User
230231
$user = new User();
231232
$user->nick = 'user_' . mt_rand(1, 999);
232233
$user->email = $user->nick . '@facturascripts.com';
233-
$user->setPassword(mt_rand(1, 999999));
234+
$user->setPassword(Tools::password());
234235

235236
return $user;
236237
}

0 commit comments

Comments
 (0)