1<?php
2
3namespace Elgg\Views;
4
5use Elgg\HttpException;
6use Elgg\IntegratedUnitTestCase;
7use Elgg\Plugins\PluginTesting;
8
9/**
10 * Abstract class for testing view output
11 */
12abstract class ViewRenderingTestCase extends IntegratedUnitTestCase {
13
14	use PluginTesting;
15
16	public function up() {
17		_elgg_services()->logger->disable();
18
19
20	}
21
22	public function down() {
23		//_elgg_services()->session->removeLoggedInUser();
24		//elgg_set_page_owner_guid(null);
25
26		_elgg_services()->logger->enable();
27	}
28
29	/**
30	 * Returns an array of view names to test with given default view vars
31	 * @return array
32	 */
33	abstract public function getViewNames();
34
35	/**
36	 * Returns default view vars to testing rendering
37	 * @return array
38	 */
39	abstract public function getDefaultViewVars();
40
41	/**
42	 * An array of views to test
43	 * @return array;
44	 */
45	public function viewListProvider() {
46		self::createApplication();
47
48		$provides = [];
49
50		$data = _elgg_services()->views->getInspectorData();
51
52		foreach ($data['locations'] as $viewtype => $views) {
53			foreach ($views as $view => $path) {
54				if (in_array($view, $this->getViewNames())) {
55					$provides[] = [$view, $viewtype];
56				}
57			}
58		}
59
60		return $provides;
61	}
62
63	/**
64	 * Assert expected view output
65	 *
66	 * @param string $expected  Expected string
67	 * @param string $view      View to test
68	 * @param array  $view_vars View vars
69	 * @param string $message   Error message
70	 *
71	 * @return void
72	 */
73	public function assertViewOutput($expected, $view, $view_vars = [], $viewtype = 'default', $message = '') {
74		$actual = $this->view($view, $view_vars, $viewtype);
75		$this->assertXmlStringEqualsXmlString($expected, $actual, $message);
76	}
77
78	/**
79	 * @dataProvider viewListProvider
80	 */
81	public function testCanRenderViewWithEmptyVars($view, $viewtype) {
82		try {
83			$output = $this->view($view, [], $viewtype);
84			$this->assertIsString($output);
85		} catch (HttpException $e) {
86
87		}
88	}
89
90	/**
91	 * @dataProvider viewListProvider
92	 */
93	public function testCanRenderViewWithVars($view, $viewtype) {
94		try {
95			$output = $this->view($view, $this->getDefaultViewVars(), $viewtype);
96			$this->assertIsString($output);
97		} catch (HttpException $e) {
98
99		}
100	}
101
102	/**
103	 * Render a view using a correct elgg_view_* function
104	 */
105	public function view($view, array $vars = [], $viewtype = 'default', array $component_vars = []) {
106		list($component, $subview) = explode('/', $view, 2);
107
108		switch ($component) {
109			case 'form' :
110				$prev_viewtype = elgg_set_viewtype($viewtype);
111				$output = elgg_view_form($subview, $component_vars, $vars);
112				elgg_set_viewtype($prev_viewtype);
113				return $output;
114
115			case 'resource' :
116				$prev_viewtype = elgg_set_viewtype($viewtype);
117				$output = elgg_view_resource($subview, $component_vars, $vars);
118				elgg_set_viewtype($prev_viewtype);
119				return $output;
120
121			default:
122				return elgg_view($view, $vars, $viewtype);
123		}
124	}
125
126}
127