<?php
//
// After including cdash_test_case.php, subsequent require_once calls are
// relative to the top of the CDash source tree
//
require_once('${testing_dir}/cdash_test_case.php');

require_once('include/pdo.php');

class ProcessSubmissionsTestCase extends KWWebTestCase
{
  function __construct()
    {
    parent::__construct();
    }

  function addFakeSubmissionRecords($projectid)
    {
    // Insert fake submission records for the given projectid.
    global $CDASH_SUBMISSION_PROCESSING_TIME_LIMIT;

    $now_utc = gmdate(FMT_DATETIMESTD);

    $i = 0;
    while ($i < 6)
      {
      pdo_query(
        "INSERT INTO submission ".
        " (filename,projectid,status,attempts,filesize,filemd5sum,created) ".
        "VALUES ".
        " ('bogus_submission_file_2.noxml','$projectid','0','0','999','bogus_md5sum_2','$now_utc')"
      );
      ++$i;
      }
    return 0;
    }

  function launchViaCurl($path, $timeout)
    {
    $request = $this->url . $path;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $request);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_exec($ch);
    curl_close($ch);
    }

  function testProperClientProcessingTest()
    {
    // We need to login to be able to see scheduleSummary.php later on.
    $this->login();

    // Get the scheduleid for the client job that's still running
    $row = pdo_single_row_query("SELECT scheduleid FROM client_job WHERE status=2");
    $scheduleid = $row[0];
    echo "scheduleid: $scheduleid\n";

    // Find some .xml files from the "simple" ctest tests that we can use for submissions.
    $baseXMLDir = "${binary_testing_dir}/ctest/simple/Testing";
    $xmlDir = "";
    if($handle = opendir($baseXMLDir))
      {
      while (false !== ($file = readdir($handle)))
        {
        if($file[0] === '2')
          {
          $xmlDir = $baseXMLDir . "/" . $file;
          break;
          }
        }
      }

    // Queue up six fake submissions to process
    $this->addFakeSubmissionRecords("5");

    // Launch the submission processor via curl and tell it to take a long time
    // by sleeping each time through its loop.
    // (With 6 fake records just added, it'll sleep for about 6 seconds,
    // 1 second for each time through its loop...)
    $this->launchViaCurl("/ajax/processsubmissions.php?projectid=5&sleep_in_loop=1", 1);

    // Sleep for 2 seconds, and then try to process a client submission.
    echo "sleeping... (1)\n";
    sleep(2);
    $url = $this->url."/submit.php?project=InsightExample&clientscheduleid=$scheduleid";
    $this->uploadfile($url,$xmlDir . "/" . "Build.xml");

    // Sleep again to give CDash a chance to process this submission before
    // we check if it is properly associated with the client job.
    echo "sleeping... (2)\n";
    sleep(5);

    // Check scheduleSummary.php to see if the client's submission got associated with
    // the correct job.
    $content = $this->get($this->url."/scheduleSummary.php?scheduleid=$scheduleid");
    if(strpos($content, "Builds Submitted For This Job") === false)
      {
      $this->fail("'Builds Submitted For This Job' not found when expected");
      return 1;
      }

    $this->pass("Passed");
    return 0;
    }
}
?>
