summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/Validators/HtmlValidator.php
blob: 66af8019f14b63c0f608dff1afa62c65903ef63c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

namespace SMW\Tests\Utils\Validators;

use DOMDocument;
use Symfony\Component\CssSelector\CssSelectorConverter;

/**
 * @license GNU GPL v2+
 * @since   3.0
 *
 * @author Stephan Gambke
 */
class HtmlValidator extends \PHPUnit_Framework_Assert {

	/**
	 * @var array
	 */
	private $documentCache = [];

	/**
	 * @var null|boolean
	 */
	private $canUse;

	/**
	 * @param string $actual
	 * @param string $message
	 */
	public function assertThatHtmlIsValid( $actual, $message = '' ) {

		$document = $this->getDomDocumentFromHtmlFragment( $actual );

		self::assertTrue( $document !== false, "Failed test `{$message}` (assertion HtmlIsValid) for $actual" );
	}

	/**
	 * @return boolean
	 */
	public function canUse() {

		if ( $this->canUse === null ) {
			$this->canUse = class_exists( '\Symfony\Component\CssSelector\CssSelectorConverter' );
		}

		return $this->canUse;
	}

	/**
	 * @param string $fragment
	 *
	 * @return bool|DOMDocument
	 */
	private function getDomDocumentFromHtmlFragment( $fragment ) {

		$cacheKey = md5( $fragment );

		if ( !isset( $this->documentCache[ $cacheKey ] ) ) {
			$this->addHtmlFragmentToCache( $fragment, $cacheKey );
		}

		return $this->documentCache[ $cacheKey ];
	}

	/**
	 * @param $fragment
	 * @param $cacheKey
	 */
	private function addHtmlFragmentToCache( $fragment, $cacheKey ) {

		$fragment = self::wrapHtmlFragment( $fragment );

		$document = new DOMDocument();
		$document->preserveWhiteSpace = false;

		libxml_use_internal_errors( true );
		$result = $document->loadHTML( $fragment );
		libxml_use_internal_errors( false );

		$this->documentCache[ $cacheKey ] = ( $result === true ) ? $document : false;

	}

	/**
	 * @param string $fragment
	 *
	 * @return string
	 */
	private static function wrapHtmlFragment( $fragment ) {
		return "<!DOCTYPE html><html><head><meta charset='utf-8'/><title>SomeTitle</title></head><body>$fragment</body></html>";
	}

	/**
	 * @param string | string[] $cssSelectors
	 * @param string $htmlFragment
	 * @param string $message
	 */
	public function assertThatHtmlContains( $cssSelectors, $htmlFragment, $message = '', $expected = true ) {

		$document = $this->getDomDocumentFromHtmlFragment( $htmlFragment );
		$xpath = new \DOMXPath( $document );
		$converter = new CssSelectorConverter();

		foreach ( $cssSelectors as $selector ) {

			if ( is_array( $selector ) ) {
				$expectedCount = array_pop( $selector );
				$expectedCountText = ( ( $expected === true) ? '' : 'not ') . $expectedCount;
				$selector = array_shift( $selector );
			} else {
				$expectedCount = false;
				$expectedCountText = ( $expected === true) ? 'at least 1' : 'none';
			}

			$message = "Failed assertion for test case `{$message}` on: \n=====\n$htmlFragment\n=====\nExpected pattern: `$selector`\n";

			try {
				// Symfony\Component\CssSelector\Exception\SyntaxErrorException: Expected selector ...
				$entries = $xpath->evaluate( $converter->toXPath( $selector ) );
				$actualCount = $entries->length;

				$message .= "Expected occurrences: {$expectedCountText}\nFound occurrences: {$actualCount}\n";

			} catch ( \Exception $e ) {
				$actualCount = 0;
				$message .= "CssSelector: " . $e->getMessage();
			}

			self::assertTrue( ( ( $expectedCount === false && $actualCount > 0 ) || ( $actualCount === $expectedCount ) ) === $expected, $message );
		}
	}

	/**
	 * @param string | string[] $cssSelectors
	 * @param string $htmlFragment
	 * @param string $message
	 */
	public function assertThatHtmlNotContains( $cssSelectors, $htmlFragment, $message = '' ) {
		$this->assertThatHtmlContains( $cssSelectors, $htmlFragment, $message, false );
	}
}