#!/usr/bin/perl # EXPLANATION FOR THIS CRAZINESS: =cut (Edited email from Whitaker, 6/30/06) When the control strip launched yesterday (early afternoon) there was a bug that caused the wrong account creation time to be accessed for each user. This means that when qualifying (according to business logic) users were evaluated to see if the control strip should be turned on for them, the answer was always 'no'. This was then saved in the database so that we wouldn't have to do the same evaluation later. Users that had problems: 1) Users who had the control strip set to 'off' automatically by the system due to the bug 2) Users who manually turned the control strip on for themselves at sometime since control strip launched, then later decided to turn it off during the affected window. Previously the 'off' state was represented by the absense of any value, so anyone with an 'off' value set in the database is someone who was migrated by the system or used the management interface to set the control strip 'off' during our error window. -- Brad Whitaker whitaker@danga.com This test ensures that all of the desired states will be set by the lazy prop fixer for controlstrip in ljcom.pl. The first test "test_lazy_forcer" makes sure that the business logic is working correctly. The second test "test_lazy_fixer" uses the large matrix down below to verify that the expected props are set based on the inputs. -revmischa =cut use strict; use Test::More 'no_plan'; use lib "$ENV{LJHOME}/cgi-bin"; require 'ljlib.pl'; require 'ljcom.pl'; use LJ::Test qw(temp_user memcache_stress); use Apache::BML; use LJ::CProd; my $launchtime = 1143165016; my $extra_tests = 1; LJ::Test::memcache_stress(sub { run_test(); }); LJ::Test::memcache_stress(sub { my $u = fresh_user(); reset_props($u, 1, 2, 3); ok($u->raw_prop('view_control_strip') == 1, "Set view_control_strip prop"); ok($u->raw_prop('show_control_strip') == 2, "Set show_control_strip prop"); ok($u->raw_prop('control_strip_forced') == 3, "Set control_strip_forced prop"); }) if $extra_tests; sub fresh_user { my $u = temp_user(); reset_props($u); return $u; } sub reset_props { my $u = shift; my ($view_cs, $show_cs, $cs_forced) = @_; $u->set_prop('view_control_strip', $view_cs); $u->set_prop('show_control_strip', $show_cs); $u->set_prop('control_strip_forced', $cs_forced); $u->do("DELETE FROM cprod WHERE userid=?", undef, $u->{userid}); } sub run_test { test_lazy_forcer(); test_lazy_fixer(); } sub test_lazy_forcer { my $u = shift || fresh_user(); my $check_prop = shift || 'view_control_strip'; my ($view_cs, $show_cs, $cs_forced) = @_; $u->set_prop($check_prop, undef); set_timecreate($u, $launchtime - 1); check_prop($u, $check_prop, 'forced', time(), "before launch date, cprod not viewed, $check_prop undef"); reset_props($u, @_); set_timecreate($u, $launchtime + 1); check_prop($u, $check_prop, 'off', -1, "after launch, cprod not viewed, $check_prop undef"); reset_props($u, @_); set_timecreate($u, $launchtime + 1); set_cprod_viewed($u); check_prop($u, $check_prop, 'off', -1, "after launch, cprod viewed, $check_prop undef"); reset_props($u, @_); set_cprod_viewed($u); set_timecreate($u, $launchtime - 1); check_prop($u, $check_prop, 'off', -1, "before launch, cprod viewed, $check_prop undef"); reset_props($u, @_); $u->set_prop('view_control_strip', 'dark'); $u->set_prop('show_control_strip', undef); $u->set_prop('control_strip_forced', -1); set_timecreate($u, $launchtime - 1); check_prop($u, $check_prop, 'dark', -1, "before launch, cprod not viewed, $check_prop dark"); } sub test_lazy_fixer { my $u = fresh_user(); # matrix of tests. Left side is what goes in, right side is what comes out. # undef on right side is magic code for "run test_lazy_forcer" my %tests = ( # view_cs show_cs cs_forced -> view_cs show_cs cs_forced "off, off, undef" => [1, undef, undef, -1], #1 "off, off_explicit, undef" => [2, 'off_explicit', 'off_explicit', -1], #3 "forced, off, 2" => [3, 'forced', 'off_explicit', 2], #4 "forced, forced, 2" => [4, 'forced', 'forced', 2], #5 "dark, off, undef" => [5, 'dark', 'off_explicit', -1], #6 "dark, off, 2" => [6, 'dark', 'off_explicit', 2], #7 "off_explicit, dark, 2" => [7, 'off_explicit', 'dark', 2], #8 "forced, off_explicit, undef" => [8, 'forced', 'off_explicit', -1], #9 "off_explicit, dark, undef" => [9 , 'off_explicit', 'dark', -1], #10 "dark, forced, 2" => [10, 'dark', 'forced', 2], #11 ); while ( my ($setup, $expected) = each %tests) { my $u = fresh_user(); my ($view_cs, $show_cs, $cs_forced) = split /\,\s+/, $setup; $cs_forced = undef if $cs_forced eq 'undef'; my ($pass, $e_view_cs, $e_show_cs, $e_cs_forced) = @$expected; if (! defined $e_view_cs || ! defined $e_show_cs) { test_lazy_forcer($u, 'view_control_strip', $view_cs, $show_cs, $cs_forced); } else { reset_props($u, $view_cs, $show_cs, $cs_forced); my $check_prop = 'view_control_strip'; check_prop($u, $check_prop, $e_view_cs, $e_cs_forced, "$pass: [$view_cs, $show_cs, $cs_forced] => [$e_view_cs, $e_show_cs, $e_cs_forced]"); reset_props($u, $view_cs, $show_cs, $cs_forced); $check_prop = 'show_control_strip'; check_prop($u, $check_prop, $e_show_cs, $e_cs_forced, "$pass: [$view_cs, $show_cs, $cs_forced] => [$e_view_cs, $e_show_cs, $e_cs_forced]"); } } } sub set_cprod_viewed { my $u = shift; LJ::CProd->inline($u, inline => 'ControlStrip'); LJ::CProd->mark_dontshow($u, 'LJ::CProd::ControlStrip'); ok(LJ::CProd::ControlStrip->has_dismissed($u), "set cprod viewed") if $extra_tests; } sub check_prop { my ($u, $check_prop, $val, $timeval, $testmsg) = @_; my $prop = $u->prop($check_prop); # force lazy prop fixer/forcer $prop = $u->raw_prop($check_prop); is($prop, $val, $testmsg); my $when = $u->prop('control_strip_forced'); my $good = 0; if ($timeval > 0 && $when > 0 && POSIX::abs($timeval - $when) < 3) { $good = 1; } else { is($when, $timeval, "control_strip_forced set correctly"); } } sub set_timecreate { my $u = shift; my $time = shift; my $dbh = LJ::get_db_writer() or die "No db"; $dbh->do("UPDATE userusage SET timecreate=FROM_UNIXTIME(?) WHERE userid=?", undef, $time, $u->{userid}); }