Merge pull request #13126 from eileenmcnaughton/export_more
[civicrm-core.git] / tests / phpunit / CRMTraits / Page / PageTestTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
42 /**
43 * Variables assigned to smarty.
44 *
45 * @var array
46 */
47 protected $smartyVariables = [];
48
49 /**
50 * @param string $content
51 * @param string $context
52 * @param string $tplName
53 * @param CRM_Core_Page $object
54 */
55 public function checkPageContent(&$content, $context, $tplName, &$object) {
56 $this->pageContent = $content;
57 // Ideally we would validate $content as valid html here.
58 // Suppress console output.
59 $content = '';
60 $this->smartyVariables = CRM_Core_Smarty::singleton()->get_template_vars();
61 }
62
63 /**
64 * Assert that the page output contains the expected strings.
65 *
66 * @param $expectedStrings
67 */
68 protected function assertPageContains($expectedStrings) {
69 foreach ($expectedStrings as $expectedString) {
70 $this->assertContains($expectedString, $this->pageContent);
71 }
72 }
73
74 /**
75 * Assert that the expected variables have been assigned to Smarty.
76 *
77 * @param $expectedVariables
78 */
79 protected function assertSmartyVariables($expectedVariables) {
80 foreach ($expectedVariables as $variableName => $expectedValue) {
81 $this->assertEquals($expectedValue, $this->smartyVariables[$variableName]);
82 }
83 }
84
85 /**
86 * Set up environment to listen for page content.
87 */
88 protected function listenForPageContent() {
89 $this->hookClass->setHook('civicrm_alterContent', [
90 $this,
91 'checkPageContent'
92 ]);
93 }
94
95 }