|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * An abstract class that all Security sniff unit tests must extend. |
| 4 | + * |
| 5 | + * A sniff unit test checks a .inc file for expected violations of a single |
| 6 | + * coding standard. Expected errors and warnings that are not found, as well as |
| 7 | + * unexpected warnings and errors, are considered test failures. |
| 8 | + * |
| 9 | + * This class will take care of setting the configuration variables in PHP_CodeSniffer |
| 10 | + * needed to test all relevant configuration combinations for each sniff in |
| 11 | + * the Security standard. |
| 12 | + * |
| 13 | + * The configuration variables set are based on the file name of a test case file. |
| 14 | + * |
| 15 | + * Naming conventions for the test case files: |
| 16 | + * SniffNameUnitTest[.CmsFramework][.ParanoiaMode].inc |
| 17 | + * |
| 18 | + * Both `[.CmsFramework]` as well as `[.ParanoiaMode]` are optional. |
| 19 | + * If neither is set, the defaults of no CmsFramework and Paranoia level 0 will be used. |
| 20 | + * |
| 21 | + * Separate test case files for different paranoia levels and different frameworks are |
| 22 | + * only needed if the sniff behaves differently based on these settings. |
| 23 | + * |
| 24 | + * - If the sniff behaviour is the same all round, just having one plain `SniffNameUnitTest.inc` |
| 25 | + * test case file will be sufficient. |
| 26 | + * - If the sniff behaviour is only dependent on one of the two configuration settings, |
| 27 | + * the other can be left out. |
| 28 | + * Examples: |
| 29 | + * - Sniff behaviour only depends on `ParanoiaMode`: `SniffNameUnitTest.[01].inc`. |
| 30 | + * - Sniff behaviour only depends on `CmsFramework`: `SniffNameUnitTest.[CmsFramework].inc`. |
| 31 | + */ |
| 32 | + |
| 33 | +namespace PHPCS_SecurityAudit\Security\Tests; |
| 34 | + |
| 35 | +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; |
| 36 | + |
| 37 | +abstract class AbstractSecurityTestCase extends AbstractSniffUnitTest |
| 38 | +{ |
| 39 | + |
| 40 | + /** |
| 41 | + * Get a list of CLI values to set before the file is tested. |
| 42 | + * |
| 43 | + * @param string $filename The name of the file being tested. |
| 44 | + * @param \PHP_CodeSniffer\Config $config The config data for the run. |
| 45 | + * |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + public function setCliValues($filename, $config) |
| 49 | + { |
| 50 | + // Set paranoia level. |
| 51 | + $paranoia = substr($filename, (strlen($filename) - 5), 1); |
| 52 | + if ($paranoia === '1') { |
| 53 | + $config->setConfigData('ParanoiaMode', 1, true); |
| 54 | + } else { |
| 55 | + $config->setConfigData('ParanoiaMode', 0, true); |
| 56 | + } |
| 57 | + |
| 58 | + // Set the CMS Framework if necessary. |
| 59 | + $firstDot = strpos($filename, '.'); |
| 60 | + $firstOffset = ($firstDot + 1); |
| 61 | + $secondDot = strpos($filename, '.', $firstOffset); |
| 62 | + |
| 63 | + $extendedExtension = ''; |
| 64 | + if ($secondDot !== false) { |
| 65 | + $extendedExtension = substr($filename, $firstOffset, ($secondDot - $firstOffset)); |
| 66 | + } |
| 67 | + |
| 68 | + switch ($extendedExtension) { |
| 69 | + case 'Drupal7': |
| 70 | + $config->setConfigData('CmsFramework', 'Drupal7', true); |
| 71 | + break; |
| 72 | + |
| 73 | + case 'Drupal8': |
| 74 | + $config->setConfigData('CmsFramework', 'Drupal8', true); |
| 75 | + break; |
| 76 | + |
| 77 | + case 'Symfony2': |
| 78 | + $config->setConfigData('CmsFramework', 'Symfony2', true); |
| 79 | + break; |
| 80 | + |
| 81 | + default: |
| 82 | + $config->setConfigData('CmsFramework', null, true); |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments