DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d5c3254b3e56)

VCS Links

Line Code
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
#!/usr/bin/perl -w
# -*- Mode: cperl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is Litmus.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Zach Lipton <zach@zachlipton.com>

use strict;

use Litmus;
use Litmus::Error;
use Litmus::DB::Product;
use Litmus::FormWidget;
use Litmus::UserAgentDetect;
use Litmus::SysConfig;
use Litmus::Auth;
use Litmus::Utils;
use Litmus::DB::Resultbug;

use CGI;
use Date::Manip;
use JSON -convert_blessed_universally;

Litmus->init();
my $c = Litmus->cgi(); 

print $c->header();

my $vars;
my $cookie =  Litmus::Auth::getCookie();
my $show_admin = Litmus::Auth::istrusted($cookie);
$vars->{"defaultemail"} = $cookie;
$vars->{"show_admin"} = $show_admin;

$vars->{'default_match_limit'} = Litmus::DB::Testcase->getDefaultMatchLimit();
$vars->{'default_num_days'} = Litmus::DB::Testcase->getDefaultNumDays();
$vars->{'default_relevance_threshold'} = Litmus::DB::Testcase->getDefaultRelevanceThreshold();

my $products = Litmus::FormWidget->getProducts($show_admin);
my $branches = Litmus::FormWidget->getBranches($show_admin);
my $testgroups = Litmus::FormWidget->getTestgroups($show_admin);
my $subgroups = Litmus::FormWidget->getSubgroups($show_admin,'sort_order');
my $platforms = Litmus::FormWidget->getUniquePlatforms($show_admin);
my $opsyses = Litmus::FormWidget->getOpsyses($show_admin);
my $locales = Litmus::FormWidget->getLocales();

my $json = new JSON;
my $products_js = $json->allow_unknown->convert_blessed->encode($products);
my $branches_js = $json->allow_unknown->convert_blessed->encode($branches);
my $testgroups_js = $json->allow_unknown->convert_blessed->encode($testgroups);
my $subgroups_js = $json->allow_unknown->convert_blessed->encode($subgroups);
my $platforms_js = $json->allow_unknown->convert_blessed->encode($platforms);
my $opsyses_js = $json->allow_unknown->convert_blessed->encode($opsyses);

$vars->{'products_js'} = $products_js;
$vars->{'branches_js'} = $branches_js;
$vars->{'testgroups_js'} = $testgroups_js;
$vars->{'subgroups_js'} = $subgroups_js;
$vars->{'platforms_js'} = $platforms_js;
$vars->{'opsyses_js'} = $opsyses_js;
$vars->{'locales'} = $locales;

if (! $c->param) {
    Litmus->template()->process("show/search_for_testcases.tmpl", $vars) || 
        internalError(Litmus->template()->error());
    exit;
} 

if ($c->param("id")) {
  my $testcase_id = $c->param("id");
  if ($testcase_id !~ /^\d+$/) {
    internalError("Testcase ID# should be a digit.");
    exit 1;
  }
  my $page = $c->param("page");
  if (!$page or $page !~ /^\d+$/) {
    $page = 1;
  }

  # Process changes to testcases:
  # Only users with canedit can edit testcases.
  my @changed;
  my $update_status = 0;
  if ($c->param("editingTestcases") && 
      Litmus::Auth::canEdit(Litmus::Auth::getCookie())) {
    # the editingTestcases param contains a comma-separated list of 
    # testcase IDs that the user has made changes to (well, has clicked 
    # the edit button for). 
    @changed = split(',' => $c->param("editingTestcases"));
    foreach my $editid (@changed) {
      my $edittest = Litmus::DB::Testcase->retrieve($editid);
      if (! $edittest) {invalidInputError("Testcase $editid does not exist")}

      $edittest->summary($c->param("summary_edit_$editid"));
      my $product = Litmus::DB::Product->retrieve($c->param("product_$editid"));
      requireField("product", $product);
      $edittest->product($product);
      $edittest->steps($c->param("steps_edit_$editid"));
      $edittest->expected_results($c->param("results_edit_$editid"));

      if ($c->param("enabled_$editid")) {
        $edittest->enabled(1);
        if ($c->param("communityenabled_$editid")) {
          $edittest->communityenabled(1);
        } else {
          $edittest->communityenabled(0);
        }
      } else {
        $edittest->enabled(0);
        $edittest->communityenabled(0);
      }
      my $r_bug_id = $c->param("regression_bug_id_$editid");
      if ($r_bug_id eq '' or $r_bug_id eq '0') {
        undef $r_bug_id;
      }
      $edittest->regression_bug_id($r_bug_id);
      #      $edittest->sort_order($c->param("sort_order_$editid"));
      $edittest->author_id(Litmus::Auth::getCurrentUser());
      $edittest->last_updated(&Date::Manip::UnixDate("now","%q"));
      $edittest->version($edittest->version()+1);

      $update_status = $edittest->update();

      my $status = "";
      my $message = "";
      if ($update_status) {
        $status = "success";
        $message = "Testcase ID# $editid updated successfully.";
      } else {
        $status = "failure";
        $message = "Unable to update testcase ID# $editid.";
      }
      $vars->{'onload'} = "toggleMessage('$status','$message');";
    }
  } elsif ($c->param("editingTestcases") && 
           ! Litmus::Auth::canEdit(Litmus::Auth::getCookie())) {
    invalidInputError("You do not have permissions to edit testcases. ");
  }

  my $testcase = Litmus::DB::Testcase->retrieve($testcase_id);

  if (! $testcase) {
    invalidInputError("\"$testcase_id\" is not a valid testcase ID.");
  }

  my @testgroups = Litmus::DB::Testgroup->search_UniqueEnabledByTestcase($testcase_id);
  my @subgroups = Litmus::DB::Subgroup->search_UniqueEnabledByTestcase($testcase_id);

  my @result_statuses = Litmus::DB::ResultStatus->retrieve_all();

  my $showallresults = $c->param("showallresults") || "";

  my @where;
  push @where, { field => 'testcase', value => $testcase_id };
  my @order_by;
  push @order_by, { field => 'created', direction => 'DESC' };
  my ($test_results,$pager) = Litmus::DB::Testresult->getTestResults(\@where,
                                                                     \@order_by,
                                                                     undef,
                                                                     $page);

  my ($status, $message);
  if ($c->param('resultSubmission')) {
    if ($c->param('resultSubmission')) {
      $status = 'success';
      $message = 'Test result submitted successfully. See the list of test results below.';
    }
  }

  $vars->{'testcase'} = $testcase;
  $vars->{'sysconfig'} = Litmus::SysConfig->getCookie($testcase->product());
  $vars->{'testgroups'} = \@testgroups;
  $vars->{'subgroups'} = \@subgroups;
  $vars->{'result_statuses'} = \@result_statuses;
  $vars->{'showallresults'} = $showallresults;
  $vars->{'test_results'} = $test_results;
  $vars->{'pager'} = $pager;

  if ($status and $message) {
    $vars->{'onload'} = "toggleMessage('$status','$message');";
  }

  Litmus->template()->process("show/show.html.tmpl", $vars) || 
    internalError(Litmus->template()->error());    

  exit;
}

if ($c->param('searchType')) {
  if ($c->param('searchType') eq 'fulltext') {
    if ($c->param("text_snippet")) {
      my $text_snippet = quotemeta($c->param("text_snippet"));
      # Upgrade to utf8 prior to search. 
      utf8::upgrade($text_snippet);
      my $match_limit = $c->param("match_limit") || 25;
      my $relevance_threshold = $c->param("relevance_threshold") || 1;
      my @testcases;
      if ($match_limit =~ /^\d+$/ and
          $relevance_threshold =~ /^[\d\.]+$/) {
          @testcases = Litmus::DB::Testcase->getFullTextMatches($text_snippet,
                                                                $match_limit,
                                                                $relevance_threshold);
      }
      $vars->{'testcases'} = \@testcases;
      # Decode text snippet again before display.
      utf8::decode($text_snippet);
      $vars->{'search_string_for_display'} = "Full-Text Search: \"$text_snippet\"";
      $vars->{'fulltext'} = 1;
    }
  } elsif ($c->param('searchType') eq 'recent') {
    if ($c->param("recently")) {
      my $recently = $c->param("recently") || "added";
      my $match_limit = $c->param("match_limit") || 25;
      my $num_days = $c->param("num_days") || Litmus::DB::Testcase->getDefaultNumDays();
      my @testcases;
      my $search_string_for_display = "";
      if ($match_limit =~ /^\d+$/ and
          $num_days =~ /^\d+$/) {
          if ($recently eq 'added') {
            @testcases = Litmus::DB::Testcase->getNewTestcases(
                                                               $num_days,
                                                               $match_limit
                                                              );
            $search_string_for_display = "Testcases added in the last $num_days days";
          } elsif ($recently eq 'changed') {
            @testcases = Litmus::DB::Testcase->getRecentlyUpdated(
                                                                  $num_days,
                                                                  $match_limit
                                                                 );
            $search_string_for_display = "Testcases changed in the last $num_days days";
          }
      }
      $vars->{'testcases'} = \@testcases;
      $vars->{'search_string_for_display'} = $search_string_for_display;
    }
  } elsif ($c->param('searchType') eq 'by_category') {
    my $product_id = $c->param("product_id");
    my $testgroup_id = $c->param("testgroup_id");
    my $subgroup_id = $c->param("subgroup_id");
    my ($product, $testgroup, $subgroup);
    my @testcases;

    if ($subgroup_id and 
        $subgroup_id ne '-Subgroup-' and 
        $subgroup_id ne '---' and
        $product_id =~ /^\d+$/ and
        $testgroup_id =~ /^\d+$/) {
      @testcases = Litmus::DB::Testcase->search_BySubgroup($subgroup_id);
      $subgroup = Litmus::DB::Subgroup->retrieve($subgroup_id);
      $testgroup = Litmus::DB::Testgroup->retrieve($testgroup_id);
      $product = Litmus::DB::Product->retrieve($product_id);
    } elsif ($testgroup_id and
             $testgroup_id ne '-Testgroup-' and 
             $testgroup_id ne '---' and
             $product_id =~ /^\d+$/) {
      @testcases = Litmus::DB::Testcase->search_ByTestgroup($testgroup_id);
      $testgroup = Litmus::DB::Testgroup->retrieve($testgroup_id);
      $product = Litmus::DB::Product->retrieve($product_id);
    } elsif ($product_id and
             $product_id ne '-Product-' and
             $product_id ne '---' and
             $product_id =~ /^\d+$/) {
      @testcases = Litmus::DB::Testcase->search(product => $product_id);
      $product = Litmus::DB::Product->retrieve($product_id);
    }
    $vars->{'testcases'} = \@testcases;
    $vars->{'search_string_for_display'} = 
      ($product ? "product: ".$product->name() : '').
        ($testgroup ? " | testgroup: ".$testgroup->name() : '').
    	  ($subgroup ? " | subgroup: ".$subgroup->name() : ''); 

  } elsif ($c->param('searchType') eq 'ungrouped') {
      my @testcases = Litmus::DB::Testcase->search_Ungrouped();
      my $search_string_for_display = "Testcases not associated with any subgroup.";
      $vars->{'testcases'} = \@testcases;
      $vars->{'search_string_for_display'} = $search_string_for_display;
  }
  $vars->{'searchType'} = quotemeta($c->param('searchType'));
}

if ($c->param('print')) {
  Litmus->template()->process("show/print_testcases.tmpl", $vars) || 
    internalError(Litmus->template()->error());
} else {
  Litmus->template()->process("show/search_for_testcases.tmpl", $vars) || 
    internalError(Litmus->template()->error());
}