Commit | Line | Data |
---|---|---|
98867798 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
98867798 | 5 | | | |
7d61e75f TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
98867798 | 9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * Trait CRMTraits_Page_PageTestTrait | |
14 | * | |
15 | * Trait for testing quickform pages in unit tests. | |
16 | */ | |
17 | trait CRMTraits_Page_PageTestTrait { | |
18 | ||
19 | /** | |
20 | * Content from the rendered page. | |
21 | * | |
22 | * @var string | |
23 | */ | |
24 | protected $pageContent; | |
25 | ||
e6507b1f | 26 | /** |
27 | * @var \CRM_Core_Page | |
28 | */ | |
29 | protected $page; | |
30 | ||
31 | /** | |
32 | * @var string | |
33 | */ | |
34 | protected $tplName; | |
35 | ||
98867798 | 36 | /** |
37 | * Variables assigned to smarty. | |
38 | * | |
39 | * @var array | |
40 | */ | |
41 | protected $smartyVariables = []; | |
42 | ||
e6507b1f | 43 | protected $context; |
44 | ||
98867798 | 45 | /** |
46 | * @param string $content | |
47 | * @param string $context | |
48 | * @param string $tplName | |
49 | * @param CRM_Core_Page $object | |
50 | */ | |
51 | public function checkPageContent(&$content, $context, $tplName, &$object) { | |
52 | $this->pageContent = $content; | |
e6507b1f | 53 | $this->tplName = $tplName; |
54 | $this->page = $object; | |
55 | $this->context = $context; | |
98867798 | 56 | // Ideally we would validate $content as valid html here. |
57 | // Suppress console output. | |
58 | $content = ''; | |
59 | $this->smartyVariables = CRM_Core_Smarty::singleton()->get_template_vars(); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Assert that the page output contains the expected strings. | |
64 | * | |
65 | * @param $expectedStrings | |
66 | */ | |
67 | protected function assertPageContains($expectedStrings) { | |
e6507b1f | 68 | unset($this->smartyVariables['config']); |
69 | unset($this->smartyVariables['session']); | |
98867798 | 70 | foreach ($expectedStrings as $expectedString) { |
e6507b1f | 71 | $this->assertContains($expectedString, $this->pageContent, print_r($this->contributions, TRUE) . print_r($this->smartyVariables, TRUE)); |
98867798 | 72 | } |
73 | } | |
74 | ||
75 | /** | |
76 | * Assert that the expected variables have been assigned to Smarty. | |
77 | * | |
78 | * @param $expectedVariables | |
79 | */ | |
80 | protected function assertSmartyVariables($expectedVariables) { | |
81 | foreach ($expectedVariables as $variableName => $expectedValue) { | |
82 | $this->assertEquals($expectedValue, $this->smartyVariables[$variableName]); | |
83 | } | |
84 | } | |
85 | ||
e6507b1f | 86 | /** |
87 | * Check an array assigned to smarty for the inclusion of the expected variables. | |
88 | * | |
89 | * @param string $variableName | |
90 | * @param $index | |
91 | * @param $expected | |
92 | */ | |
93 | protected function assertSmartyVariableArrayIncludes($variableName, $index, $expected) { | |
94 | $smartyVariable = $this->smartyVariables[$variableName]; | |
95 | if ($index !== NULL) { | |
96 | $smartyVariable = $smartyVariable[$index]; | |
97 | } | |
98 | foreach ($expected as $key => $value) { | |
99 | $this->assertEquals($value, $smartyVariable[$key], 'Checking ' . $key); | |
100 | } | |
101 | } | |
102 | ||
98867798 | 103 | /** |
104 | * Set up environment to listen for page content. | |
105 | */ | |
106 | protected function listenForPageContent() { | |
107 | $this->hookClass->setHook('civicrm_alterContent', [ | |
108 | $this, | |
39b959db | 109 | 'checkPageContent', |
98867798 | 110 | ]); |
111 | } | |
112 | ||
113 | } |