98867798 |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 5 | |
5 | +--------------------------------------------------------------------+ |
f299f7db |
6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
98867798 |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | |
9 | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
11 | | under the terms of the GNU Affero General Public License | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
13 | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
17 | | See the GNU Affero General Public License for more details. | |
18 | | | |
19 | | You should have received a copy of the GNU Affero General Public | |
20 | | License and the CiviCRM Licensing Exception along | |
21 | | with this program; if not, contact CiviCRM LLC | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
25 | +--------------------------------------------------------------------+ |
26 | */ |
27 | |
28 | /** |
29 | * Trait CRMTraits_Page_PageTestTrait |
30 | * |
31 | * Trait for testing quickform pages in unit tests. |
32 | */ |
33 | trait CRMTraits_Page_PageTestTrait { |
34 | |
35 | /** |
36 | * Content from the rendered page. |
37 | * |
38 | * @var string |
39 | */ |
40 | protected $pageContent; |
41 | |
e6507b1f |
42 | /** |
43 | * @var \CRM_Core_Page |
44 | */ |
45 | protected $page; |
46 | |
47 | /** |
48 | * @var string |
49 | */ |
50 | protected $tplName; |
51 | |
98867798 |
52 | /** |
53 | * Variables assigned to smarty. |
54 | * |
55 | * @var array |
56 | */ |
57 | protected $smartyVariables = []; |
58 | |
e6507b1f |
59 | protected $context; |
60 | |
98867798 |
61 | /** |
62 | * @param string $content |
63 | * @param string $context |
64 | * @param string $tplName |
65 | * @param CRM_Core_Page $object |
66 | */ |
67 | public function checkPageContent(&$content, $context, $tplName, &$object) { |
68 | $this->pageContent = $content; |
e6507b1f |
69 | $this->tplName = $tplName; |
70 | $this->page = $object; |
71 | $this->context = $context; |
98867798 |
72 | // Ideally we would validate $content as valid html here. |
73 | // Suppress console output. |
74 | $content = ''; |
75 | $this->smartyVariables = CRM_Core_Smarty::singleton()->get_template_vars(); |
76 | } |
77 | |
78 | /** |
79 | * Assert that the page output contains the expected strings. |
80 | * |
81 | * @param $expectedStrings |
82 | */ |
83 | protected function assertPageContains($expectedStrings) { |
e6507b1f |
84 | unset($this->smartyVariables['config']); |
85 | unset($this->smartyVariables['session']); |
98867798 |
86 | foreach ($expectedStrings as $expectedString) { |
e6507b1f |
87 | $this->assertContains($expectedString, $this->pageContent, print_r($this->contributions, TRUE) . print_r($this->smartyVariables, TRUE)); |
98867798 |
88 | } |
89 | } |
90 | |
91 | /** |
92 | * Assert that the expected variables have been assigned to Smarty. |
93 | * |
94 | * @param $expectedVariables |
95 | */ |
96 | protected function assertSmartyVariables($expectedVariables) { |
97 | foreach ($expectedVariables as $variableName => $expectedValue) { |
98 | $this->assertEquals($expectedValue, $this->smartyVariables[$variableName]); |
99 | } |
100 | } |
101 | |
e6507b1f |
102 | /** |
103 | * Check an array assigned to smarty for the inclusion of the expected variables. |
104 | * |
105 | * @param string $variableName |
106 | * @param $index |
107 | * @param $expected |
108 | */ |
109 | protected function assertSmartyVariableArrayIncludes($variableName, $index, $expected) { |
110 | $smartyVariable = $this->smartyVariables[$variableName]; |
111 | if ($index !== NULL) { |
112 | $smartyVariable = $smartyVariable[$index]; |
113 | } |
114 | foreach ($expected as $key => $value) { |
115 | $this->assertEquals($value, $smartyVariable[$key], 'Checking ' . $key); |
116 | } |
117 | } |
118 | |
98867798 |
119 | /** |
120 | * Set up environment to listen for page content. |
121 | */ |
122 | protected function listenForPageContent() { |
123 | $this->hookClass->setHook('civicrm_alterContent', [ |
124 | $this, |
39b959db |
125 | 'checkPageContent', |
98867798 |
126 | ]); |
127 | } |
128 | |
129 | } |