|
| 1 | +<?php |
| 2 | +/** |
| 3 | +* |
| 4 | +* Google Analytics extension for the phpBB Forum Software package. |
| 5 | +* |
| 6 | +* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com> |
| 7 | +* @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | +* |
| 9 | +*/ |
| 10 | + |
| 11 | +namespace phpbb\googleanalytics\tests\event; |
| 12 | + |
| 13 | +class event_listener_test extends \phpbb_test_case |
| 14 | +{ |
| 15 | + /** @var \phpbb\googleanalytics\event\listener */ |
| 16 | + protected $listener; |
| 17 | + |
| 18 | + /** |
| 19 | + * Setup test environment |
| 20 | + * |
| 21 | + * @access public |
| 22 | + */ |
| 23 | + public function setUp() |
| 24 | + { |
| 25 | + parent::setUp(); |
| 26 | + |
| 27 | + global $phpbb_dispatcher; |
| 28 | + |
| 29 | + // Mock some global classes that may be called during code execution |
| 30 | + $phpbb_dispatcher = new \phpbb_mock_event_dispatcher(); |
| 31 | + |
| 32 | + // Load/Mock classes required by the event listener class |
| 33 | + $this->config = new \phpbb\config\config(array('googleanalytics_id' => 'UA-000000-01')); |
| 34 | + $this->template = new \phpbb\googleanalytics\tests\mock\template(); |
| 35 | + $this->user = new \phpbb\user('\phpbb\datetime'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Create our event listener |
| 40 | + * |
| 41 | + * @access protected |
| 42 | + */ |
| 43 | + protected function set_listener() |
| 44 | + { |
| 45 | + $this->listener = new \phpbb\googleanalytics\event\listener( |
| 46 | + $this->config, |
| 47 | + $this->template, |
| 48 | + $this->user |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test the event listener is constructed correctly |
| 54 | + * |
| 55 | + * @access public |
| 56 | + */ |
| 57 | + public function test_construct() |
| 58 | + { |
| 59 | + $this->set_listener(); |
| 60 | + $this->assertInstanceOf('\Symfony\Component\EventDispatcher\EventSubscriberInterface', $this->listener); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test the event listener is subscribing events |
| 65 | + * |
| 66 | + * @access public |
| 67 | + */ |
| 68 | + public function test_getSubscribedEvents() |
| 69 | + { |
| 70 | + $this->assertEquals(array( |
| 71 | + 'core.page_header', |
| 72 | + 'core.acp_board_config_edit_add', |
| 73 | + 'core.validate_config_variable', |
| 74 | + ), array_keys(\phpbb\googleanalytics\event\listener::getSubscribedEvents())); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test the load_google_analytics event |
| 79 | + * |
| 80 | + * @access public |
| 81 | + */ |
| 82 | + public function test_load_google_analytics() |
| 83 | + { |
| 84 | + $this->set_listener(); |
| 85 | + |
| 86 | + $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); |
| 87 | + $dispatcher->addListener('core.page_header', array($this->listener, 'load_google_analytics')); |
| 88 | + $dispatcher->dispatch('core.page_header'); |
| 89 | + |
| 90 | + $this->assertEquals(array( |
| 91 | + 'GOOGLEANALYTICS_ID' => $this->config['googleanalytics_id'], |
| 92 | + ), $this->template->get_template_vars()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Data set for test_validate_googleanalytics_id |
| 97 | + * |
| 98 | + * @return array Array of test data |
| 99 | + * @access public |
| 100 | + */ |
| 101 | + public function validate_googleanalytics_id_data() |
| 102 | + { |
| 103 | + return array( |
| 104 | + array( |
| 105 | + // valid code, no error |
| 106 | + array('googleanalytics_id' => 'UA-0000-00'), |
| 107 | + array(), |
| 108 | + ), |
| 109 | + array( |
| 110 | + // no code, no error |
| 111 | + array('googleanalytics_id' => ''), |
| 112 | + array(), |
| 113 | + ), |
| 114 | + array( |
| 115 | + // invalid code, error |
| 116 | + array('googleanalytics_id' => 'UA-00-00'), |
| 117 | + array('ACP_GOOGLEANALYTICS_ID_INVALID'), |
| 118 | + ), |
| 119 | + array( |
| 120 | + // invalid code, error |
| 121 | + array('googleanalytics_id' => 'UA-00000-00000'), |
| 122 | + array('ACP_GOOGLEANALYTICS_ID_INVALID'), |
| 123 | + ), |
| 124 | + array( |
| 125 | + // invalid code, error |
| 126 | + array('googleanalytics_id' => 'AU-0000-00'), |
| 127 | + array('ACP_GOOGLEANALYTICS_ID_INVALID'), |
| 128 | + ), |
| 129 | + array( |
| 130 | + // invalid code, error |
| 131 | + array('googleanalytics_id' => 'foo-bar-foo'), |
| 132 | + array('ACP_GOOGLEANALYTICS_ID_INVALID'), |
| 133 | + ), |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test the validate_googleanalytics_id event |
| 139 | + * |
| 140 | + * @dataProvider validate_googleanalytics_id_data |
| 141 | + * @access public |
| 142 | + */ |
| 143 | + public function test_validate_googleanalytics_id($cfg_array, $expected_error) |
| 144 | + { |
| 145 | + $this->set_listener(); |
| 146 | + |
| 147 | + $config_name = 'googleanalytics_id'; |
| 148 | + $config_definition = array('validate' => 'googleanalytics_id'); |
| 149 | + $error = array(); |
| 150 | + |
| 151 | + $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); |
| 152 | + $dispatcher->addListener('core.validate_config_variable', array($this->listener, 'validate_googleanalytics_id')); |
| 153 | + |
| 154 | + $event_data = array('cfg_array', 'config_name', 'config_definition', 'error'); |
| 155 | + $event = new \phpbb\event\data(compact($event_data)); |
| 156 | + $dispatcher->dispatch('core.validate_config_variable', $event); |
| 157 | + |
| 158 | + $event_data_after = $event->get_data_filtered($event_data); |
| 159 | + foreach ($event_data as $expected) |
| 160 | + { |
| 161 | + $this->assertArrayHasKey($expected, $event_data_after); |
| 162 | + } |
| 163 | + extract($event_data_after); |
| 164 | + |
| 165 | + $this->assertEquals($expected_error, $error); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Data set for test_add_googleanalytics_configs |
| 170 | + * |
| 171 | + * @return array Array of test data |
| 172 | + * @access public |
| 173 | + */ |
| 174 | + public function add_googleanalytics_configs_data() |
| 175 | + { |
| 176 | + return array( |
| 177 | + array( // expected config and mode |
| 178 | + 'settings', |
| 179 | + array('vars' => array('override_user_style' => array())), |
| 180 | + array('override_user_style', 'googleanalytics_id'), |
| 181 | + ), |
| 182 | + array( // unexpected mode |
| 183 | + 'foobar', |
| 184 | + array('vars' => array('override_user_style' => array())), |
| 185 | + array('override_user_style'), |
| 186 | + ), |
| 187 | + array( // unexpected config |
| 188 | + 'settings', |
| 189 | + array('vars' => array('foobar' => array())), |
| 190 | + array('foobar'), |
| 191 | + ), |
| 192 | + array( // unexpected config and mode |
| 193 | + 'foobar', |
| 194 | + array('vars' => array('foobar' => array())), |
| 195 | + array('foobar'), |
| 196 | + ), |
| 197 | + ); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Test the add_googleanalytics_configs event |
| 202 | + * |
| 203 | + * @dataProvider add_googleanalytics_configs_data |
| 204 | + * @access public |
| 205 | + */ |
| 206 | + public function test_add_googleanalytics_configs($mode, $display_vars, $expected_keys) |
| 207 | + { |
| 208 | + $this->set_listener(); |
| 209 | + |
| 210 | + $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); |
| 211 | + $dispatcher->addListener('core.acp_board_config_edit_add', array($this->listener, 'add_googleanalytics_configs')); |
| 212 | + |
| 213 | + $event_data = array('display_vars', 'mode'); |
| 214 | + $event = new \phpbb\event\data(compact($event_data)); |
| 215 | + $dispatcher->dispatch('core.acp_board_config_edit_add', $event); |
| 216 | + |
| 217 | + $event_data_after = $event->get_data_filtered($event_data); |
| 218 | + foreach ($event_data as $expected) |
| 219 | + { |
| 220 | + $this->assertArrayHasKey($expected, $event_data_after); |
| 221 | + } |
| 222 | + extract($event_data_after); |
| 223 | + |
| 224 | + $keys = array_keys($display_vars['vars']); |
| 225 | + |
| 226 | + $this->assertEquals($expected_keys, $keys); |
| 227 | + } |
| 228 | +} |
0 commit comments