$case['perm']); $_POST = $case['post']; $_REQUEST = $case['post']; $view = 'options'; $tab = 'API'; ob_start(); include __DIR__.'/../../web/skins/classic/views/_options_api.php'; $html = ob_get_clean(); echo json_encode(array('queries' => $GLOBALS['queries'], 'warnings' => $GLOBALS['warnings'], 'html' => $html)); exit(0); } function render_api_tab($systemPerm, array $post) { $arg = json_encode(array('perm' => $systemPerm, 'post' => (object)$post)); $out = shell_exec(escapeshellarg(PHP_BINARY).' '.escapeshellarg(__FILE__).' '.escapeshellarg($arg)); $r = json_decode($out, true); if (!is_array($r)) { echo "child failed: $out\n"; exit(1); } $GLOBALS['queries'] = $r['queries']; $GLOBALS['warnings'] = $r['warnings']; return $r['html']; } $writes = array( 'updateSelected' => array('updateSelected' => '', 'apiUids' => array('1')), 'revokeAllTokens' => array('revokeAllTokens' => ''), ); foreach ($writes as $name => $post) { $html = render_api_tab('View', $post); check("System=View $name sends no SQL", $GLOBALS['queries'], array()); check("System=View $name logs a warning", count($GLOBALS['warnings']), 1); check("System=View $name page still renders", strpos($html, 'contentTable') !== false, true); render_api_tab('Edit', $post); check("System=Edit $name updates Users", count($GLOBALS['queries']) > 0 && strpos($GLOBALS['queries'][0], 'UPDATE `Users`') === 0, true); } $html = render_api_tab('View', array()); check('System=View sees the write buttons disabled', preg_match_all('/id="(updateSelected|revokeAllTokens)" disabled="disabled"/', $html), 2); check('System=View sees the per-user checkboxes disabled', preg_match_all('/name="(tokenUids|apiUids)\[\]"[^>]*disabled="disabled"/', $html), 2); $html = render_api_tab('Edit', array()); check('System=Edit sees the write buttons enabled', substr_count($html, 'disabled="disabled"'), 0); render_api_tab('Edit', array('revokeAllTokens' => '', 'updateSelected' => '')); check('System=Edit with both buttons runs both handlers', count(array_filter($GLOBALS['queries'], function($q) { return strpos($q, 'TokenMinExpiry') !== false; })) >= 1 && count(array_filter($GLOBALS['queries'], function($q) { return strpos($q, 'APIEnabled') !== false; })) >= 1, true); echo "\n$passes passed, $failures failed\n"; exit($failures ? 1 : 0); }