This file is indexed.

/usr/share/php/kohana3.1/modules/unittest/classes/controller/unittest.php is in libkohana3.1-mod-unittest-php 3.1.5-1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php defined('SYSPATH') or die ('No direct script access.');
/**
 * PHPUnit Kohana web based test runner
 *
 * @package    Kohana/UnitTest
 * @author     Kohana Team
 * @author     BRMatt <matthew@sigswitch.com>
 * @author     Paul Banks
 * @copyright  (c) 2008-2009 Kohana Team
 * @license    http://kohanaphp.com/license
 */

class Controller_UnitTest extends Controller_Template
{
	/**
	 * Whether the archive module is available
	 * @var boolean
	 */
	protected $cc_archive_available = FALSE;

	/**
	 * Unittest config
	 * @var Config
	 */
	protected $config = NULL;

	/**
	 * The uri by which the report uri will be executed
	 * @var string
	 */
	protected $report_uri = '';

	/**
	 * The uri by which the run action will be executed
	 * @var string
	 */
	protected $run_uri = '';

	/**
	 * Is the XDEBUG extension loaded?
	 * @var boolean
	 */
	protected $xdebug_loaded = FALSE;

	/**
	 * Template
	 * @var string
	 */
	public $template = 'unittest/layout';

	/**
	 * Loads test suite
	 */
	public function before()
	{
		parent::before();

		if ( ! Unittest_tests::enabled())
		{
			// Pretend this is a normal 404 error...
			$this->status = 404;

			throw new Kohana_Request_Exception('Unable to find a route to match the URI: :uri',
				array(':uri' => $this->request->uri()));
		}

		// Prevent the whitelist from being autoloaded, but allow the blacklist
		// to be loaded
		Unittest_Tests::configure_environment(FALSE);

		$this->config = Kohana::config('unittest');

		// This just stops some very very long lines
		$route = Route::get('unittest');
		$this->report_uri = $route->uri(array('action' => 'report'));
		$this->run_uri = $route->uri(array('action' => 'run'));

		// Switch used to disable cc settings
		$this->xdebug_loaded = extension_loaded('xdebug');
		$this->cc_archive_enabled = class_exists('Archive');

		Kohana_View::set_global('xdebug_enabled', $this->xdebug_loaded);
		Kohana_View::set_global('cc_archive_enabled', $this->cc_archive_enabled);
	}

	/**
	 * Handles index page for /unittest/ and /unittest/index/
	 */
	public function action_index()
	{
		$this->template->body = View::factory('unittest/index')
			->set('run_uri', $this->run_uri)
			->set('report_uri', $this->report_uri)
			->set('whitelistable_items', $this->get_whitelistable_items())
			->set('groups', $this->get_groups_list(Unittest_tests::suite()));
	}

	/**
	 * Handles report generation
	 */
	public function action_report()
	{
		// Fairly foolproof
		if ( ! $this->config->cc_report_path AND ! class_exists('Archive'))
			throw new Kohana_Exception('Cannot generate report');

		// We don't want to use the HTML layout, we're sending the user 100111011100110010101100
		$this->auto_render = FALSE;

		$suite = Unittest_tests::suite();
		$temp_path = rtrim($this->config->temp_path, '/').'/';
		$group = (array) Arr::get($_GET, 'group', array());

		// Stop unittest from interpretting "all groups" as "no groups"
		if (empty($group) OR empty($group[0]))
		{
			$group = array();
		}

		if (Arr::get($_GET, 'use_whitelist', FALSE))
		{
			$this->whitelist(Arr::get($_GET, 'whitelist', array()));
		}

		$runner = new Kohana_Unittest_Runner($suite);

		// If the user wants to download a report
		if ($this->cc_archive_enabled AND Arr::get($_GET, 'archive') === '1')
		{
			// $report is the actual directory of the report,
			// $folder is the name component of directory
			list($report, $folder) = $runner->generate_report($group, $temp_path);

			$archive = Archive::factory('zip');

			// TODO: Include the test results?
			$archive->add($report, 'report', TRUE);

			$filename = $folder.'.zip';

			$archive->save($temp_path.$filename);

			// It'd be nice to clear up afterwards but by deleting the report dir we corrupt the archive
			// And once the archive has been sent to the user Request stops the script so we can't delete anything
			// It'll be up to the user to delete files periodically
			$this->request->send_file($temp_path.$filename, $filename);
		}
		else
		{
			$folder = trim($this->config->cc_report_path, '/').'/';
			$path = DOCROOT.$folder;

			if ( ! file_exists($path))
				throw new Kohana_Exception('Report directory :dir does not exist', array(':dir' => $path));

			if ( ! is_writable($path))
				throw new Kohana_Exception('Script doesn\'t have permission to write to report dir :dir ', array(':dir' => $path));

			$runner->generate_report($group, $path, FALSE);

			$this->request->redirect(URL::site($folder.'index.html', $this->request));
		}
	}

	/**
	 * Handles test running interface
	 */
	public function action_run()
	{
		$this->template->body = View::factory('unittest/results');

		// Get the test suite and work out which groups we're testing
		$suite = Unittest_tests::suite();
		$group = (array) Arr::get($_GET, 'group', array());


		// Stop phpunit from interpretting "all groups" as "no groups"
		if (empty($group) OR empty($group[0]))
		{
			$group = array();
		}

		// Only collect code coverage if the user asked for it
		$collect_cc = (bool) Arr::get($_GET, 'collect_cc', FALSE);

		if ($collect_cc AND Arr::get($_GET, 'use_whitelist', FALSE))
		{
			$whitelist = $this->whitelist(Arr::get($_GET, 'whitelist', array()));
		}

		$runner = new Kohana_Unittest_Runner($suite);

		try
		{
			$runner->run($group, $collect_cc);

			if ($collect_cc)
			{
				$this->template->body->set('coverage', $runner->calculate_cc_percentage());
			}

			if (isset($whitelist))
			{
				$this->template->body->set('coverage_explanation', $this->nice_whitelist_explanation($whitelist));
			}
		}
		catch(Kohana_Exception $e)
		{
			// Code coverage is not allowed, possibly xdebug disabled?
			// TODO: Tell the user this?
			$runner->run($group);
		}

		// Show some results
		$this->template->body
			->set('results', $runner->results)
			->set('totals', $runner->totals)
			->set('time', $this->nice_time($runner->time))

			// Sets group to the currently selected group, or default all groups
			->set('group', Arr::get($this->get_groups_list($suite), reset($group), 'All groups'))
			->set('groups', $this->get_groups_list($suite))

			->set('run_uri', $this->request->uri())
			->set('report_uri', $this->report_uri.url::query())

			// Whitelist related stuff
			->set('whitelistable_items', $this->get_whitelistable_items())
			->set('whitelisted_items', isset($whitelist) ? array_keys($whitelist) : array())
			->set('whitelist', ! empty($whitelist));
	}

	/**
	 * Get the list of groups from the test suite, sorted with 'All groups' prefixed
	 *
	 * @return array Array of groups in the test suite
	 */
	protected function get_groups_list($suite)
	{
		// Make groups aray suitable for drop down
		$groups = $suite->getGroups();
		if (count($groups) > 0)
		{
			sort($groups);
			$groups = array_combine($groups, $groups);
		}
		return array('' => 'All Groups') + $groups;
	}

	/**
	 * Gets a list of items that are whitelistable
	 *
	 * @return array
	 */
	protected function get_whitelistable_items()
	{
		static $whitelist;

		if (count($whitelist))
			return $whitelist;

		$whitelist = array();

		$whitelist['k_app'] = 'Application';

		$k_modules = array_keys(Kohana::modules());

		$whitelist += array_map('ucfirst', array_combine($k_modules, $k_modules));

		$whitelist['k_sys'] = 'Kohana Core';

		return $whitelist;
	}

	/**
	 * Whitelists a specified set of modules specified by the user
	 *
	 * @param array $modules
	 */
	protected function whitelist(array $modules)
	{
		$k_modules = Kohana::modules();
		$whitelist = array();

		// Make sure our whitelist is valid
		foreach ($modules as $item)
		{
			if (isset($k_modules[$item]))
			{
				$whitelist[$item] = $k_modules[$item];
			}
			elseif ($item === 'k_app')
			{
				$whitelist[$item] = APPPATH;
			}
			elseif ($item === 'k_sys')
			{
				$whitelist[$item] = SYSPATH;
			}
		}

		if (count($whitelist))
		{
			Unittest_tests::whitelist($whitelist);
		}

		return $whitelist;
	}

	/**
	 * Prettifies the list of whitelisted modules
	 *
	 * @param array Array of whitelisted items
	 * @return string
	 */
	protected function nice_whitelist_explanation(array $whitelist)
	{
		$items = array_intersect_key($this->get_whitelistable_items(), $whitelist);

		return implode(', ', $items);
	}

	protected function nice_time($time)
	{
		$parts = array();

		if ($time > DATE::DAY)
		{
			$parts[] = floor($time/DATE::DAY).'d';
			$time = $time % DATE::DAY;
		}

		if ($time > DATE::HOUR)
		{
			$parts[] = floor($time/DATE::HOUR).'h';
			$time = $time % DATE::HOUR;
		}

		if ($time > DATE::MINUTE)
		{
			$parts[] = floor($time/DATE::MINUTE).'m';
			$time = $time % DATE::MINUTE;
		}

		if ($time > 0)
		{
			$parts[] = round($time, 1).'s';
		}

		return implode(' ', $parts);
	}
} // End Controller_PHPUnit