|
| 1 | +#!/usr/bin/env perl |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | +# |
| 6 | +# This Source Code Form is "Incompatible With Secondary Licenses", as |
| 7 | +# defined by the Mozilla Public License, v. 2.0. |
| 8 | +use strict; |
| 9 | +use warnings; |
| 10 | +use 5.10.1; |
| 11 | +use lib qw( . lib local/lib/perl5 ); |
| 12 | + |
| 13 | +BEGIN { $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf' } |
| 14 | + |
| 15 | +use Bugzilla; |
| 16 | +BEGIN { Bugzilla->extensions } |
| 17 | + |
| 18 | +use Bugzilla::Constants; |
| 19 | +use Bugzilla::Extension::TrackingFlags::Flag; |
| 20 | +use Test2::V0; |
| 21 | + |
| 22 | +# Tracking flag lookups by name should be served from the request cache when |
| 23 | +# the flag is already loaded, and fall back to the database otherwise. |
| 24 | + |
| 25 | +my $FLAG = 'Bugzilla::Extension::TrackingFlags::Flag'; |
| 26 | +my @db_lookups; |
| 27 | +my $mock_object = mock 'Bugzilla::Object' => ( |
| 28 | + override => [ |
| 29 | + new => sub { |
| 30 | + my ($class, $param) = @_; |
| 31 | + push @db_lookups, $param; |
| 32 | + return undef; |
| 33 | + } |
| 34 | + ] |
| 35 | +); |
| 36 | + |
| 37 | +sub flag { bless {id => $_[0], name => $_[1]}, $FLAG } |
| 38 | + |
| 39 | +sub new_request { |
| 40 | + my (%flags) = @_; |
| 41 | + Bugzilla->clear_request_cache; |
| 42 | + Bugzilla->request_cache->{tracking_flags} |
| 43 | + = {map { $_->flag_id => $_ } values %flags} |
| 44 | + if %flags; |
| 45 | + @db_lookups = (); |
| 46 | +} |
| 47 | + |
| 48 | +subtest 'cached flag found by name without a query' => sub { |
| 49 | + my $a = flag(1, 'cf_status_firefox140'); |
| 50 | + new_request(a => $a); |
| 51 | + is($FLAG->new({name => 'cf_status_firefox140'}), exact_ref($a), |
| 52 | + 'cached object'); |
| 53 | + is($FLAG->new({name => 'CF_STATUS_FIREFOX140'}), |
| 54 | + exact_ref($a), 'case-insensitive, like the database lookup'); |
| 55 | + is(scalar @db_lookups, 0, 'no database lookups'); |
| 56 | +}; |
| 57 | + |
| 58 | +subtest 'unknown name falls back to the database' => sub { |
| 59 | + new_request(a => flag(1, 'cf_status_firefox140')); |
| 60 | + is($FLAG->new({name => 'cf_rank'}), undef, 'not a tracking flag'); |
| 61 | + is(\@db_lookups, [{name => 'cf_rank'}], 'one database lookup'); |
| 62 | +}; |
| 63 | + |
| 64 | +subtest 'no request cache: database lookup' => sub { |
| 65 | + new_request(); |
| 66 | + $FLAG->new({name => 'cf_status_firefox140'}); |
| 67 | + is(scalar @db_lookups, 1, 'database lookup'); |
| 68 | +}; |
| 69 | + |
| 70 | +subtest 'index follows changes to the cache' => sub { |
| 71 | + my $a = flag(1, 'cf_status_firefox140'); |
| 72 | + new_request(a => $a); |
| 73 | + $FLAG->new({name => 'cf_status_firefox140'}); # builds the index |
| 74 | + |
| 75 | + # A flag added to the cache later (e.g. by update()) is found. |
| 76 | + my $b = flag(2, 'cf_status_firefox141'); |
| 77 | + Bugzilla->request_cache->{tracking_flags}{2} = $b; |
| 78 | + is($FLAG->new({name => 'cf_status_firefox141'}), exact_ref($b), 'new flag'); |
| 79 | + |
| 80 | + # A renamed flag is not returned under its old name. |
| 81 | + $a->{name} = 'cf_status_firefox_renamed'; |
| 82 | + is($FLAG->new({name => 'cf_status_firefox140'}), undef, 'old name'); |
| 83 | + is(scalar @db_lookups, 1, 'old name went to the database'); |
| 84 | + |
| 85 | + # A flag removed from the cache (remove_from_db()) is not returned. |
| 86 | + delete Bugzilla->request_cache->{tracking_flags}{2}; |
| 87 | + @db_lookups = (); |
| 88 | + is($FLAG->new({name => 'cf_status_firefox141'}), undef, 'removed flag'); |
| 89 | + is(scalar @db_lookups, 1, 'removed flag went to the database'); |
| 90 | +}; |
| 91 | + |
| 92 | +subtest 'id lookups are unchanged' => sub { |
| 93 | + my $a = flag(1, 'cf_status_firefox140'); |
| 94 | + new_request(a => $a); |
| 95 | + is($FLAG->new(1), exact_ref($a), 'cached by id'); |
| 96 | + is(scalar @db_lookups, 0, 'no database lookups'); |
| 97 | +}; |
| 98 | + |
| 99 | +subtest 'bug_check_can_change_field skips non-tracking fields' => sub { |
| 100 | + my $a = flag(1, 'cf_status_firefox140'); |
| 101 | + new_request(a => $a); |
| 102 | + my $mock_names = mock 'Bugzilla' => |
| 103 | + (override => [tracking_flag_names => sub {'cf_status_firefox140'}]); |
| 104 | + my $mock_flag = mock $FLAG => (override => [can_set_value => sub {1}]); |
| 105 | + |
| 106 | + my @results; |
| 107 | + my $check = sub { |
| 108 | + my ($field) = @_; |
| 109 | + Bugzilla::Extension::TrackingFlags->bug_check_can_change_field({ |
| 110 | + bug => undef, |
| 111 | + field => $field, |
| 112 | + old_value => '---', |
| 113 | + new_value => 'fixed', |
| 114 | + priv_results => \@results, |
| 115 | + }); |
| 116 | + }; |
| 117 | + |
| 118 | + $check->('cf_rank'); |
| 119 | + is(\@results, [], 'non-tracking field: no result'); |
| 120 | + is(\@db_lookups, [], 'non-tracking field: no database lookup'); |
| 121 | + |
| 122 | + $check->('cf_status_firefox140'); |
| 123 | + is(\@results, [{result => PRIVILEGES_REQUIRED_NONE}], 'tracking flag checked'); |
| 124 | + is(\@db_lookups, [], 'tracking flag served from the request cache'); |
| 125 | +}; |
| 126 | + |
| 127 | +done_testing; |
0 commit comments