|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * --------------------------------------------------------------------- |
| 5 | + * |
| 6 | + * GLPI - Gestionnaire Libre de Parc Informatique |
| 7 | + * |
| 8 | + * http://glpi-project.org |
| 9 | + * |
| 10 | + * @copyright 2015-2025 Teclib' and contributors. |
| 11 | + * @licence https://www.gnu.org/licenses/gpl-3.0.html |
| 12 | + * |
| 13 | + * --------------------------------------------------------------------- |
| 14 | + * |
| 15 | + * LICENSE |
| 16 | + * |
| 17 | + * This file is part of GLPI. |
| 18 | + * |
| 19 | + * This program is free software: you can redistribute it and/or modify |
| 20 | + * it under the terms of the GNU General Public License as published by |
| 21 | + * the Free Software Foundation, either version 3 of the License, or |
| 22 | + * (at your option) any later version. |
| 23 | + * |
| 24 | + * This program is distributed in the hope that it will be useful, |
| 25 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | + * GNU General Public License for more details. |
| 28 | + * |
| 29 | + * You should have received a copy of the GNU General Public License |
| 30 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 31 | + * |
| 32 | + * --------------------------------------------------------------------- |
| 33 | + */ |
| 34 | + |
| 35 | +namespace tests\units; |
| 36 | + |
| 37 | +use CommonDBChild; |
| 38 | +use Computer; |
| 39 | +use DbTestCase; |
| 40 | + |
| 41 | +class CommonDBChildTest extends DbTestCase |
| 42 | +{ |
| 43 | + public function testPrepareInputForAddWithMandatoryFkeyRelation(): void |
| 44 | + { |
| 45 | + $instance = new class extends CommonDBChild { |
| 46 | + public static $itemtype = Computer::class; |
| 47 | + public static $items_id = 'computers_id'; |
| 48 | + public static $mustBeAttached = true; |
| 49 | + |
| 50 | + public static $disableAutoEntityForwarding = true; // prevent DB accesses |
| 51 | + }; |
| 52 | + |
| 53 | + $this->assertFalse($instance->prepareInputForAdd(['foo' => 'bar'])); |
| 54 | + $this->hasSessionMessages(ERROR, ['Parent item Computer #null is invalid.']); |
| 55 | + |
| 56 | + $this->assertFalse($instance->prepareInputForAdd(['computers_id' => 9999999999, 'foo' => 'bar'])); |
| 57 | + $this->hasSessionMessages(ERROR, ['Parent item Computer #9999999999 is invalid.']); |
| 58 | + |
| 59 | + $valid_id = \getItemByTypeName(Computer::class, '_test_pc01', true); |
| 60 | + $this->assertEquals( |
| 61 | + ['computers_id' => $valid_id, 'foo' => 'bar'], |
| 62 | + $instance->prepareInputForAdd(['computers_id' => $valid_id, 'foo' => 'bar']) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function testPrepareInputForAddWithOptionalFkeyRelation(): void |
| 67 | + { |
| 68 | + $instance = new class extends CommonDBChild { |
| 69 | + public static $itemtype = Computer::class; |
| 70 | + public static $items_id = 'computers_id'; |
| 71 | + public static $mustBeAttached = false; |
| 72 | + |
| 73 | + public static $disableAutoEntityForwarding = true; // prevent DB accesses |
| 74 | + }; |
| 75 | + |
| 76 | + $this->assertEquals( |
| 77 | + ['foo' => 'bar'], |
| 78 | + $instance->prepareInputForAdd(['foo' => 'bar']) |
| 79 | + ); |
| 80 | + |
| 81 | + $this->assertEquals( |
| 82 | + ['computers_id' => 0, 'foo' => 'bar'], |
| 83 | + $instance->prepareInputForAdd(['computers_id' => 9999999999, 'foo' => 'bar']) |
| 84 | + ); |
| 85 | + |
| 86 | + $valid_id = \getItemByTypeName(Computer::class, '_test_pc01', true); |
| 87 | + $this->assertEquals( |
| 88 | + ['computers_id' => $valid_id, 'foo' => 'bar'], |
| 89 | + $instance->prepareInputForAdd(['computers_id' => $valid_id, 'foo' => 'bar']) |
| 90 | + ); |
| 91 | + } |
| 92 | + public function testPrepareInputForAddWithMandatoryPolymorphicRelation(): void |
| 93 | + { |
| 94 | + $instance = new class extends CommonDBChild { |
| 95 | + public static $itemtype = 'itemtype'; |
| 96 | + public static $items_id = 'items_id'; |
| 97 | + public static $mustBeAttached = true; |
| 98 | + |
| 99 | + public static $disableAutoEntityForwarding = true; // prevent DB accesses |
| 100 | + }; |
| 101 | + |
| 102 | + $this->assertFalse($instance->prepareInputForAdd(['foo' => 'bar'])); |
| 103 | + $this->hasSessionMessages(ERROR, ['Parent item null #null is invalid.']); |
| 104 | + |
| 105 | + $this->assertFalse($instance->prepareInputForAdd(['itemtype' => 'Computer', 'foo' => 'bar'])); |
| 106 | + $this->hasSessionMessages(ERROR, ['Parent item Computer #null is invalid.']); |
| 107 | + |
| 108 | + $this->assertFalse($instance->prepareInputForAdd(['itemtype' => 'NotAClass', 'foo' => 'bar'])); |
| 109 | + $this->hasSessionMessages(ERROR, ['Parent item NotAClass #null is invalid.']); |
| 110 | + |
| 111 | + $this->assertFalse($instance->prepareInputForAdd(['itemtype' => 'Computer', 'items_id' => 9999999999, 'foo' => 'bar'])); |
| 112 | + $this->hasSessionMessages(ERROR, ['Parent item Computer #9999999999 is invalid.']); |
| 113 | + |
| 114 | + $valid_id = \getItemByTypeName(Computer::class, '_test_pc01', true); |
| 115 | + $this->assertEquals( |
| 116 | + ['itemtype' => 'Computer', 'items_id' => $valid_id, 'foo' => 'bar'], |
| 117 | + $instance->prepareInputForAdd(['itemtype' => 'Computer', 'items_id' => $valid_id, 'foo' => 'bar']) |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + public function testPrepareInputForAddWithOptionalPolymorphicRelation(): void |
| 122 | + { |
| 123 | + $instance = new class extends CommonDBChild { |
| 124 | + public static $itemtype = 'itemtype'; |
| 125 | + public static $items_id = 'items_id'; |
| 126 | + public static $mustBeAttached = false; |
| 127 | + |
| 128 | + public static $disableAutoEntityForwarding = true; // prevent DB accesses |
| 129 | + }; |
| 130 | + |
| 131 | + $this->assertEquals( |
| 132 | + ['foo' => 'bar'], |
| 133 | + $instance->prepareInputForAdd(['foo' => 'bar']) |
| 134 | + ); |
| 135 | + |
| 136 | + $this->assertEquals( |
| 137 | + ['items_id' => 0, 'foo' => 'bar'], |
| 138 | + $instance->prepareInputForAdd(['items_id' => 9999999999, 'foo' => 'bar']) |
| 139 | + ); |
| 140 | + |
| 141 | + $this->assertEquals( |
| 142 | + ['itemtype' => '', 'foo' => 'bar'], |
| 143 | + $instance->prepareInputForAdd(['itemtype' => 'NotAClass', 'foo' => 'bar']) |
| 144 | + ); |
| 145 | + |
| 146 | + $this->assertEquals( |
| 147 | + ['itemtype' => '', 'items_id' => 0, 'foo' => 'bar'], |
| 148 | + $instance->prepareInputForAdd(['itemtype' => 'Computer', 'items_id' => 9999999999, 'foo' => 'bar']) |
| 149 | + ); |
| 150 | + |
| 151 | + $valid_id = \getItemByTypeName(Computer::class, '_test_pc01', true); |
| 152 | + $this->assertEquals( |
| 153 | + ['itemtype' => 'Computer', 'items_id' => $valid_id, 'foo' => 'bar'], |
| 154 | + $instance->prepareInputForAdd(['itemtype' => 'Computer', 'items_id' => $valid_id, 'foo' => 'bar']) |
| 155 | + ); |
| 156 | + } |
| 157 | +} |
0 commit comments