|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * ------------------------------------------------------------------------- |
| 5 | + * DataInjection plugin for GLPI |
| 6 | + * ------------------------------------------------------------------------- |
| 7 | + * |
| 8 | + * LICENSE |
| 9 | + * |
| 10 | + * This file is part of DataInjection. |
| 11 | + * |
| 12 | + * DataInjection is free software; you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License as published by |
| 14 | + * the Free Software Foundation; either version 2 of the License, or |
| 15 | + * (at your option) any later version. |
| 16 | + * |
| 17 | + * DataInjection is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU General Public License |
| 23 | + * along with DataInjection. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * ------------------------------------------------------------------------- |
| 25 | + * @copyright Copyright (C) 2007-2023 by DataInjection plugin team. |
| 26 | + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html |
| 27 | + * @link https://github.com/pluginsGLPI/datainjection |
| 28 | + * ------------------------------------------------------------------------- |
| 29 | + */ |
| 30 | + |
| 31 | +require_once dirname(__DIR__, 2) . '/inc/clientinjection.class.php'; |
| 32 | + |
| 33 | +/** |
| 34 | + * Covers escapeCsvFormula(), which prefixes values starting with a CSV |
| 35 | + * formula-injection trigger character with a single quote before they are |
| 36 | + * written out by exportErrorsInCSV(). |
| 37 | + */ |
| 38 | +class ClientInjectionEscapeCsvFormulaTest extends DbTestCase |
| 39 | +{ |
| 40 | + public function escapeCsvFormulaProvider(): array |
| 41 | + { |
| 42 | + return [ |
| 43 | + 'empty string' => ['', ''], |
| 44 | + 'equals trigger' => ['=SUM(A1:A2)', "'=SUM(A1:A2)"], |
| 45 | + 'plus trigger' => ['+1234', "'+1234"], |
| 46 | + 'minus trigger' => ['-1234', "'-1234"], |
| 47 | + 'at trigger' => ['@SUM(A1:A2)', "'@SUM(A1:A2)"], |
| 48 | + 'safe value passthrough' => ['normal value', 'normal value'], |
| 49 | + 'non-string passthrough' => [42, 42], |
| 50 | + ]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @dataProvider escapeCsvFormulaProvider |
| 55 | + */ |
| 56 | + public function testEscapeCsvFormula($value, $expected): void |
| 57 | + { |
| 58 | + $escape_csv_formula = new ReflectionMethod( |
| 59 | + PluginDatainjectionClientInjection::class, |
| 60 | + 'escapeCsvFormula' |
| 61 | + ); |
| 62 | + $this->assertSame($expected, $escape_csv_formula->invoke(null, $value)); |
| 63 | + } |
| 64 | +} |
0 commit comments