1<?php
2
3/*
4	Phoronix Test Suite
5	URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
6	Copyright (C) 2015 - 2018, Phoronix Media
7	Copyright (C) 2015 - 2018, Michael Larabel
8
9	This program is free software; you can redistribute it and/or modify
10	it under the terms of the GNU General Public License as published by
11	the Free Software Foundation; either version 3 of the License, or
12	(at your option) any later version.
13
14	This program is distributed in the hope that it will be useful,
15	but WITHOUT ANY WARRANTY; without even the implied warranty of
16	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17	GNU General Public License for more details.
18
19	You should have received a copy of the GNU General Public License
20	along with this program. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23
24class phoromatic_build_suite implements pts_webui_interface
25{
26	public static function page_title()
27	{
28		return 'Build Custom Test Suite';
29	}
30	public static function page_header()
31	{
32		return null;
33	}
34	public static function preload($PAGE)
35	{
36		return true;
37	}
38	public static function render_page_process($PATH)
39	{
40		if(isset($_POST['suite_title']))
41		{
42		//	echo '<pre>';
43		//	var_dump($_POST);
44		//	echo '</pre>';
45
46			if(strlen($_POST['suite_title']) < 3)
47			{
48				echo '<h2>Suite title must be at least three characters.</h2>';
49			}
50
51			//echo 'TEST SUITE: ' . $_POST['suite_title'] . '<br />';
52			//echo 'TEST SUITE: ' . $_POST['suite_description'] . '<br />';
53			$tests = array();
54
55			foreach($_POST['test_add'] as $i => $test_identifier)
56			{
57				$test_prefix = $_POST['test_prefix'][$i];
58				$args = array();
59				$args_name = array();
60
61				foreach($_POST as $i => $v)
62				{
63					if(strpos($i, $test_prefix) !== false && substr($i, -9) != '_selected')
64					{
65						if(strpos($v, '||') !== false)
66						{
67							$opts = explode('||', $v);
68							$a = array();
69							$d = array();
70							foreach($opts as $opt)
71							{
72								$t = explode('::', $opt);
73								$a[] = $t[1];
74								$d[] = $t[0];
75							}
76							$args[] = $a;
77							$args_name[] = $d;
78						}
79						else
80						{
81							$args[] = array($v);
82							$args_name[] = array($_POST[$i . '_selected']);
83						}
84					}
85				}
86
87				$test_args = array();
88				$test_args_description = array();
89				pts_test_run_options::compute_all_combinations($test_args, null, $args, 0);
90				pts_test_run_options::compute_all_combinations($test_args_description, null, $args_name, 0, ' - ');
91
92				foreach(array_keys($test_args) as $i)
93				{
94					$tests[] = array('test' => $test_identifier, 'description' => $test_args_description[$i], 'args' => $test_args[$i]);
95				}
96			}
97
98			if(count($tests) < 1)
99			{
100				echo '<h2>You must add at least one test to the suite.</h2>';
101			}
102
103			$new_suite = new pts_test_suite();
104			$version_bump = 0;
105
106		//	do
107		//	{
108				//$suite_version = '1.' . $version_bump . '.0';
109				$suite_version = $_POST['suite_version'];
110				$suite_id = $new_suite->clean_save_name_string($_POST['suite_title']) . '-' . $suite_version;
111				$suite_dir = phoromatic_server::phoromatic_account_suite_path($_SESSION['AccountID'], $suite_id);
112		//		$version_bump++;
113		//	}
114		//	while(is_dir($suite_dir));
115			pts_file_io::mkdir($suite_dir);
116			$save_to = $suite_dir . '/suite-definition.xml';
117
118			$new_suite->set_title($_POST['suite_title']);
119			$new_suite->set_version($suite_version); // $suite_version
120			$new_suite->set_maintainer($_SESSION['UserName']);
121			$new_suite->set_suite_type('System');
122			$new_suite->set_description($_POST['suite_description']);
123
124			foreach($tests as $m)
125			{
126				$new_suite->add_to_suite($m['test'], $m['args'], $m['description']);
127			}
128
129			$new_suite->save_xml(null, $save_to);
130			echo '<h2>Saved As ' . $suite_id . '</h2>';
131		}
132		echo phoromatic_webui_header_logged_in();
133		$main = '<h1>Local Suites</h1><p>Find already created local test suites by your account/group via the <a href="/?local_suites">local suites</a> page.</p>';
134
135
136		if(!PHOROMATIC_USER_IS_VIEWER)
137		{
138			$suite = null;
139			if(isset($PATH[0]))
140			{
141				$suite = phoromatic_server::phoromatic_account_suite_path($_SESSION['AccountID'], $PATH[0]) . '/suite-definition.xml';
142				if(!is_file($suite))
143				{
144					$suite = null;
145				}
146			}
147			$suite = new pts_test_suite($suite);
148
149			$main .= '<h1>Build Suite</h1><p>A test suite in the realm of the Phoronix Test Suite, OpenBenchmarking.org, and Phoromatic is <strong>a collection of test profiles with predefined settings</strong>. Establishing a test suite makes it easy to run repetitive testing on the same set of test profiles by simply referencing the test suite name.</p>';
150			$main .= '<form action="' . $_SERVER['REQUEST_URI'] . '" name="build_suite" id="build_suite" method="post" onsubmit="return validate_suite();">
151			<h3>Title:</h3>
152			<p><input type="text" name="suite_title" value="' . $suite->get_title() . '" /></p>
153			<h3>Suite Version:</h3>
154			<p><input type="text" name="suite_version" value="' . ($suite->get_version() == null ? '1.0.0' : $suite->get_version()) . '" /></p>
155			<h3>Description:</h3>
156			<p><textarea name="suite_description" id="suite_description" cols="60" rows="2">' . $suite->get_description() . '</textarea></p>
157			<h3>Tests In Schedule:</h3>
158			<p><div id="test_details"></div></p>
159			<script type="text/javascript">';
160
161			foreach($suite->get_contained_test_result_objects() as $obj)
162			{
163				$main .= 'phoromatic_ajax_append_element("r_add_test_build_suite_details/&tp=' . $obj->test_profile->get_identifier() . '&tpa=' . $obj->get_arguments_description() . '", "test_details");' . PHP_EOL;
164			}
165			$main .= '</script>
166			<h3>Add Another Test</h3>';
167			$main .= '<select name="add_to_suite_select_test" id="add_to_suite_select_test" onchange="phoromatic_build_suite_test_details();"><option value=""></option>';
168			$dc = pts_client::download_cache_path();
169			$dc_exists = is_file($dc . 'pts-download-cache.json');
170			if($dc_exists)
171			{
172				$cache_json = file_get_contents($dc . 'pts-download-cache.json');
173				$cache_json = json_decode($cache_json, true);
174			}
175			foreach(array_merge(pts_tests::local_tests(), pts_openbenchmarking::available_tests(false, true)) as $test)
176			{
177				$cache_checked = false;
178				if(phoromatic_server::read_setting('show_local_tests_only'))
179				{
180					if($dc_exists)
181					{
182						if($cache_json && isset($cache_json['phoronix-test-suite']['cached-tests']))
183						{
184							$cache_checked = true;
185							if(!in_array($test, $cache_json['phoronix-test-suite']['cached-tests']))
186							{
187								continue;
188							}
189						}
190					}
191					if(!$cache_checked && phoromatic_server::read_setting('show_local_tests_only') && pts_test_install_request::test_files_available_on_local_system($test) == false)
192					{
193						continue;
194					}
195				}
196				$main .= '<option value="' . $test . '">' . $test . '</option>';
197			}
198			$main .= '</select>';
199			$main .= '<p align="right"><input name="submit" value="' . ($suite->get_title() != null ? 'Edit' : 'Create') .' Suite" type="submit" onclick="return pts_rmm_validate_suite();" /></p>';
200		}
201
202		echo '<div id="pts_phoromatic_main_area">' . $main . '</div>';
203		echo phoromatic_webui_footer();
204	}
205}
206
207?>
208