de-randomization fixes for testing, #37340
authorMark Wells <mark@freeside.biz>
Tue, 4 Aug 2015 18:16:41 +0000 (11:16 -0700)
committerMark Wells <mark@freeside.biz>
Tue, 4 Aug 2015 18:22:05 +0000 (11:22 -0700)
Conflicts:
FS/FS/Conf.pm

13 files changed:
FS/FS/Conf.pm
FS/FS/Mason.pm
FS/FS/Record.pm
FS/FS/UI/Web.pm
httemplate/elements/bill.html
httemplate/elements/columnstart.html
httemplate/elements/create_uri_query
httemplate/elements/input-fcc_options.html
httemplate/elements/pickcolor.html
httemplate/elements/popup_link_onclick.html
httemplate/elements/progress-init.html
httemplate/elements/tr-input-mask.html
httemplate/graph/elements/report.html

index c936082..53459c7 100644 (file)
@@ -5922,6 +5922,13 @@ and customer address. Include units.',
     'type'        => 'text',
   },
 
+  {
+    'key'         => 'no_random_ids',
+    'section'     => '',
+    'description' => 'Replace random identifiers in UI code with a static string, for repeatable testing. Don\'t use in production.',
+    'type'        => 'checkbox',
+  },
+
 );
 
 1;
index 3d577f6..ae4f07c 100644 (file)
@@ -136,7 +136,7 @@ if ( -e $addl_handler_use_file ) {
   use FS::Conf;
   use FS::CGI qw(header menubar table itable ntable idiot
                  eidiot myexit http_header);
-  use FS::UI::Web qw(svc_url);
+  use FS::UI::Web qw(svc_url random_id);
   use FS::UI::Web::small_custview qw(small_custview);
   use FS::UI::bytecount;
   use FS::UI::REST qw( rest_auth rest_uri_remain encode_rest );
index 70d4f67..d6892a9 100644 (file)
@@ -527,6 +527,7 @@ sub qsearch {
 
     # Check for encrypted fields and decrypt them.
    ## only in the local copy, not the cached object
+    no warnings 'deprecated'; # XXX silence the warning for now
     if ( $conf_encryption 
          && eval '@FS::'. $table . '::encrypted_fields' ) {
       foreach my $record (@return) {
index 6655f27..13b2e2d 100644 (file)
@@ -15,11 +15,13 @@ use FS::cust_main;  # are sql_balance and sql_date_balance in the right module?
 #@ISA = qw( FS::UI );
 @ISA = qw( Exporter );
 
-@EXPORT_OK = qw( svc_url );
+@EXPORT_OK = qw( svc_url random_id );
 
 $DEBUG = 0;
 $me = '[FS::UID::Web]';
 
+our $NO_RANDOM_IDS;
+
 ###
 # date parsing
 ###
@@ -608,6 +610,35 @@ sub is_mobile {
   return 0;
 }
 
+=item random_id [ DIGITS ]
+
+Returns a random number of length DIGITS, or if unspecified, a long random 
+identifier consisting of the timestamp, process ID, and a random number.
+Anything in the UI that needs a random identifier should use this.
+
+=cut
+
+sub random_id {
+  my $digits = shift;
+  if (!defined $NO_RANDOM_IDS) {
+    my $conf = FS::Conf->new;
+    $NO_RANDOM_IDS = $conf->exists('no_random_ids') ? 1 : 0;
+  }
+  if ( $NO_RANDOM_IDS ) {
+    if ( $digits > 0 ) {
+      return 0;
+    } else {
+      return '0000000000-0000-000000000.000000';
+    }
+  } else {
+    if ($digits > 0) {
+      return int(rand(10 ** $digits));
+    } else {
+      return time . "-$$-" . rand() * 2**32;
+    }
+  }
+}
+
 =back
 
 =cut
index 64a1a6d..420a748 100644 (file)
@@ -45,7 +45,7 @@ my $label     = $opt{'label'};
 # formname no longer needs to be passed from outside, but we still 
 # need one and it needs to be unique
 my $formname  = $opt{'formname'} ||
-                'bill'.sprintf('%04d',int(rand(10000))).$custnum;
+                'bill'.sprintf('%04d',random_id(4)).$custnum;
 my $url       = $opt{'url'} || '';
 my $message   = $opt{'message'} || 'Finished!';
 my $bill_opts = $opt{'bill_opts'} || {};
index 1ffbcb9..245c308 100644 (file)
@@ -10,7 +10,7 @@
 
 Pass 'aligned' => 1 to have corresponding rows in the columns line up.
 </%doc>
-% my $id = sprintf('table%08d', rand(100000000));
+% my $id = sprintf('table%08d', random_id(8));
 <TR>
   <TD CLASS="background" COLSPAN=99>
     <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 id="<%$id%>">
index ce6249e..414d53b 100644 (file)
@@ -18,7 +18,7 @@ my $query = $cgi->query_string;
 
 if ( length($query) > 1920 || $opt{secure} ) { #stupid IE 2083 URL limit
 
-  my $session = int(rand(4294967296)); #XXX
+  my $session = random_id(9);
   my $pref = new FS::access_user_pref({
     'usernum'    => $FS::CurrentUser::CurrentUser->usernum,
     'prefname'   => "redirect$session",
index 064c647..080b40f 100644 (file)
@@ -9,7 +9,7 @@
 % }
 % unless ($opt{html_only}) {
 %   my $popup = $fsurl.'misc/part_pkg_fcc_options.html?id=';
-%   my $popup_name = 'popup-'.time. "-$$-". rand() * 2**32;
+%   my $popup_name = 'popup-'.random_id();
 <SCRIPT TYPE="text/javascript">
 function edit_fcc_options() {
   var id = this.dataset['target'];
index d410ebf..2b0647f 100644 (file)
@@ -38,7 +38,7 @@ my %opt = @_;
 
 my $value = length($opt{curr_value}) ? $opt{curr_value} : $opt{value};
 
-my $unum = int(rand(100000));
+my $unum = random_id(5);
 
 my $id = $opt{'id'} || $opt{'field'}.$unum;
 
index 0690dc8..612de75 100644 (file)
@@ -62,7 +62,7 @@ $scrolling   = $params->{'scrolling'}   if exists $params->{'scrolling'};
 
 #stupid safari is caching the "location" of popup iframs, and submitting them
 #instead of displaying them.  this should prevent that.
-my $popup_name = 'popup-'.time. "-$$-". rand() * 2**32;
+my $popup_name = 'popup-'.random_id();
 
 my $onclick =
   "overlib( OLiframeContent($action, $width, $height, '$popup_name', 0, '$scrolling' ), ".
index 5b42aa1..2728240 100644 (file)
@@ -170,6 +170,6 @@ $progress_url->query_form(
 
 #stupid safari is caching the "location" of popup iframs, and submitting them
 #instead of displaying them.  this should prevent that.
-my $popup_name = 'popup-'.time. "-$$-". rand() * 2**32;
+my $popup_name = 'popup-'.random_id();
 
 </%init>
index fdd2096..93e322c 100644 (file)
@@ -68,7 +68,7 @@ my $init = 0;
 <%init>
 my %opt = @_;
 # must have a DOM id
-my $id = $opt{id} || sprintf('input%04d',int(rand(10000)));
+my $id = $opt{id} || sprintf('input%04d',random_id(4));
 my $value = length($opt{curr_value}) ? $opt{curr_value} : $opt{value} || '';
 
 my $clipboard_hack = $FS::CurrentUser::CurrentUser->option('enable_mask_clipboard_hack');
index cffc828..f1b0d16 100644 (file)
@@ -331,7 +331,7 @@ if ( $cgi->param('session') =~ /^(\d+)$/ ) {
   %opt = %{ $m->cache->get($session) };
 }
 else {
-  $session = sprintf("%010d%06d", time, int(rand(1000000)));
+  $session = sprintf("%010d", random_id(10));
   $m->cache->set($session, \%opt, '1h');
 }