HtaccessEnabledTester   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
eloc 30
c 4
b 0
f 0
dl 0
loc 92
ccs 30
cts 30
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerTestFiles() 0 2 1
A getSubDir() 0 3 1
B run() 0 60 10
1
<?php
2
3
namespace HtaccessCapabilityTester\Testers;
4
5
use \HtaccessCapabilityTester\HtaccessCapabilityTester;
6
use \HtaccessCapabilityTester\TestResult;
7
8
/**
9
 * Class for testing if .htaccess files are processed
10
 *
11
 * @package    HtaccessCapabilityTester
12
 * @author     Bjørn Rosell <[email protected]>
13
 * @since      Class available since 0.7
14
 */
15
class HtaccessEnabledTester extends AbstractTester
16
{
17
18
    /**
19
     * Child classes must implement this method, which tells which subdir the
20
     * test files are to be put.
21
     *
22
     * @return  string  A subdir for the test files
23
     */
24 22
    public function getSubDir()
25
    {
26 22
        return 'htaccess-enabled';
27
    }
28
29
    /**
30
     * Register the test files using the "registerTestFile" method
31
     *
32
     * @return  void
33
     */
34 22
    public function registerTestFiles()
35
    {
36
        // No test files for this test
37 22
    }
38
39
    /**
40
     *  Run the test.
41
     *
42
     * @param  string  $baseDir  Directory on the server where the test files can be put
43
     * @param  string  $baseUrl  The base URL of the test files
44
     *
45
     * @return TestResult   Returns a test result
46
     */
47 22
    public function run($baseDir, $baseUrl)
48
    {
49 22
        $this->prepareForRun($baseDir, $baseUrl);
50
51
        /*
52
        PS: We could implement this as a definition:
53
54
55
        - [success, serverSignatureWorks, is-success]
56
        - [success, contentDigestWorks, is-success]
57
        - [failure, serverSignatureWorks, is-failure]
58
        - [success, canCrash, is-success]
59
        */
60
61
62 22
        $status = null;
63 22
        $info = '';
64 22
        $hct = $this->getHtaccessCapabilityTester();
65
66
        // If we can find anything that works, well the .htaccess must have been proccesed!
67 22
        if ($hct->serverSignatureWorks()    // Override: None,  Status: Core, REQUIRES PHP
68 19
            || $hct->contentDigestWorks()   // Override: Options,  Status: Core
69 17
            || $hct->addTypeWorks()         // Override: FileInfo, Status: Base, Module: mime
70 15
            || $hct->directoryIndexWorks()  // Override: Indexes,  Status: Base, Module: mod_dir
71 13
            || $hct->rewriteWorks()         // Override: FileInfo, Status: Extension, Module: rewrite
72 22
            || $hct->headerSetWorks()       // Override: FileInfo, Status: Extension, Module: headers
73
        ) {
74 16
            $status = true;
75
        } else {
76
            // The serverSignatureWorks() test is special because if it comes out as a failure,
77
            // we can be *almost* certain that the .htaccess has been completely disabled
78
79 6
            $serverSignatureWorks = $hct->serverSignatureWorks();
80 6
            if ($serverSignatureWorks === false) {
81 1
                $status = false;
82 1
                $info = 'ServerSignature directive does not work - and it is in core';
83
            } else {
84
                // Last bullet in the gun:
85
                // Try an .htaccess with syntax errors in it.
86
                // (we do this lastly because it may generate an entry in the error log)
87 5
                $crashTestResult = $hct->crashTest('aoeu', 'htaccess-enabled-malformed-htaccess');
88 5
                if (is_null($crashTestResult)) {
89
                    // Two scenarios:
90
                    // 1: All requests fails (without response code)
91
                    // 2: The crash test could not figure it out (ie if even innocent requests crashes)
92 2
                    $status = null;
93 2
                    $info = 'all requests fails (even innocent ones)';
94 3
                } elseif ($crashTestResult === false) {
95
                    // It crashed, - which means .htaccess is processed!
96 1
                    $status = true;
97 1
                    $info = 'syntax error in an .htaccess causes crash';
98
                } else {
99
                    // It did not crash. So the .htaccess is not processed, as syntax errors
100
                    // makes servers crash
101 2
                    $status = false;
102 2
                    $info = 'syntax error in an .htaccess does not cause crash';
103
                }
104
            }
105
        }
106 22
        return new TestResult($status, $info);
107
    }
108
}
109