forked from yllen/pdf
-
Notifications
You must be signed in to change notification settings - Fork 12
FIX / PDF export for assignable asset groups #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mary-Clb
wants to merge
15
commits into
main
Choose a base branch
from
fix/pdf-assignable-groups-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
63a67af
fix PDF export for assignable asset groups
Mary-Clb c5a4367
update CHANGELOG.md
Mary-Clb c6ee945
remove helpers & use getItemsAssociatedTo()
Mary-Clb 9d121a6
fix rector
Mary-Clb 8561d73
refactor: consolidate group name retrieval into PluginPdfCommon::getG…
Mary-Clb 64516c8
fix phpstan
Mary-Clb be909f2
fix phpstan
Mary-Clb 1e1b4f5
simplify getGroupName group ids retrieval
Mary-Clb 8231424
handle many-to-many groups
Mary-Clb be60c34
fix rector error
Mary-Clb b705cc7
fix header
Mary-Clb f06f353
fix phpStan error
Mary-Clb 460a46c
rm group duplicate
Mary-Clb d4e7ce8
add phphunit tests
Mary-Clb e71b3a5
fix phpstan error
Mary-Clb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <phpunit bootstrap="tests/bootstrap.php" colors="true" testdox="true"> | ||
| <testsuites> | ||
| <testsuite name="Tests"> | ||
| <directory>tests</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
| </phpunit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * ------------------------------------------------------------------------- | ||
| * LICENSE | ||
| * | ||
| * This file is part of PDF plugin for GLPI. | ||
| * | ||
| * PDF is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * PDF is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with Reports. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| * @author Nelly Mahu-Lasson, Remi Collet, Teclib | ||
| * @copyright Copyright (c) 2009-2022 PDF plugin team | ||
| * @license AGPL License 3.0 or (at your option) any later version | ||
| * @link https://github.com/pluginsGLPI/pdf/ | ||
| * @link http://www.glpi-project.org/ | ||
| * @package pdf | ||
| * @since 2009 | ||
| * http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||
| * -------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| namespace GlpiPlugin\Pdf\Tests\Units; | ||
|
|
||
| use Computer; | ||
| use Glpi\Tests\DbTestCase; | ||
| use Group; | ||
| use Group_Item; | ||
| use ITILCategory; | ||
| use PluginPdfCommon; | ||
| use ReflectionMethod; | ||
|
|
||
| final class GetGroupNameTest extends DbTestCase | ||
| { | ||
| private function getGroupName(\CommonDBTM $item, int $group_type = Group_Item::GROUP_TYPE_NORMAL): string | ||
| { | ||
| $method = new ReflectionMethod(PluginPdfCommon::class, 'getGroupName'); | ||
| $method->setAccessible(true); | ||
|
|
||
| return $method->invoke(null, $item, $group_type); | ||
| } | ||
|
|
||
| /** | ||
| * One-to-one relation: the group id is stored directly in the `groups_id` column | ||
| * (e.g. glpi_itilcategories), as a scalar value. | ||
| */ | ||
| public function testScalarGroupOnOneToOneRelation(): void | ||
| { | ||
| $group = $this->createItem(Group::class, [ | ||
| 'name' => 'One-to-one group', | ||
| 'entities_id' => $this->getTestRootEntity(true), | ||
| ]); | ||
|
|
||
| $category = $this->createItem(ITILCategory::class, [ | ||
| 'name' => 'Category with a group', | ||
| 'entities_id' => $this->getTestRootEntity(true), | ||
| 'groups_id' => $group->getID(), | ||
| ]); | ||
| $category->getFromDB($category->getID()); | ||
|
|
||
| $this->assertSame($group->fields['name'], $this->getGroupName($category)); | ||
| } | ||
|
|
||
| public function testNoGroupOnOneToOneRelation(): void | ||
| { | ||
| $category = $this->createItem(ITILCategory::class, [ | ||
| 'name' => 'Category without a group', | ||
| 'entities_id' => $this->getTestRootEntity(true), | ||
| ]); | ||
| $category->getFromDB($category->getID()); | ||
|
|
||
| $this->assertSame('', $this->getGroupName($category)); | ||
| } | ||
|
|
||
| /** | ||
| * Many-to-many relation: groups are stored in the `glpi_groups_items` pivot table | ||
| * (AssignableItem trait, e.g. glpi_computers), as an array of ids. | ||
| */ | ||
| public function testArrayGroupsOnManyToManyRelation(): void | ||
| { | ||
| $group1 = $this->createItem(Group::class, [ | ||
| 'name' => 'Computer group 1', | ||
| 'entities_id' => $this->getTestRootEntity(true), | ||
| ]); | ||
| $group2 = $this->createItem(Group::class, [ | ||
| 'name' => 'Computer group 2', | ||
| 'entities_id' => $this->getTestRootEntity(true), | ||
| ]); | ||
| $tech_group = $this->createItem(Group::class, [ | ||
| 'name' => 'Computer tech group', | ||
| 'entities_id' => $this->getTestRootEntity(true), | ||
| ]); | ||
|
|
||
| $computer = $this->createItem(Computer::class, [ | ||
| 'name' => 'Computer with groups', | ||
| 'entities_id' => $this->getTestRootEntity(true), | ||
| 'groups_id' => [$group1->getID(), $group2->getID()], | ||
| 'groups_id_tech' => [$tech_group->getID()], | ||
| ]); | ||
| $computer->getFromDB($computer->getID()); | ||
|
|
||
| $this->assertSame( | ||
| implode(', ', [$group1->fields['name'], $group2->fields['name']]), | ||
| $this->getGroupName($computer) | ||
| ); | ||
| $this->assertSame( | ||
| $tech_group->fields['name'], | ||
| $this->getGroupName($computer, Group_Item::GROUP_TYPE_TECH) | ||
| ); | ||
| } | ||
|
|
||
| public function testNoGroupsOnManyToManyRelation(): void | ||
| { | ||
| $computer = $this->createItem(Computer::class, [ | ||
| 'name' => 'Computer without groups', | ||
| 'entities_id' => $this->getTestRootEntity(true), | ||
| ]); | ||
| $computer->getFromDB($computer->getID()); | ||
|
|
||
| $this->assertSame('', $this->getGroupName($computer)); | ||
| $this->assertSame('', $this->getGroupName($computer, Group_Item::GROUP_TYPE_TECH)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * ------------------------------------------------------------------------- | ||
| * LICENSE | ||
| * | ||
| * This file is part of PDF plugin for GLPI. | ||
| * | ||
| * PDF is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * PDF is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with Reports. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| * @author Nelly Mahu-Lasson, Remi Collet, Teclib | ||
| * @copyright Copyright (c) 2009-2022 PDF plugin team | ||
| * @license AGPL License 3.0 or (at your option) any later version | ||
| * @link https://github.com/pluginsGLPI/pdf/ | ||
| * @link http://www.glpi-project.org/ | ||
| * @package pdf | ||
| * @since 2009 | ||
| * http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||
| * -------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| require __DIR__ . '/../../../tests/bootstrap.php'; | ||
|
|
||
| if (!Plugin::isPluginActive('pdf')) { | ||
| throw new RuntimeException('Plugin pdf is not active in the test database'); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There’s something I’m not fully grasping in this PR.
In GLPI, an object can be assigned to a group using a one-to-one relationship (i.e., the object contains a
groups_idorgroups_id_techcolumn, for example inglpi_itilcategories).Objects can also be assigned to multiple groups through an intermediate relation table,
glpi_groups_items(cf:glpi_computers).These objects rely on that relation table when they use the
AssignableItemtrait.Here (unless I’m mistaken), we are only handling the first case.