File: 44-patrol.t

package info (click to toggle)
libmediawiki-bot-perl 5.007000-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 656 kB
  • sloc: perl: 1,992; makefile: 5
file content (72 lines) | stat: -rw-r--r-- 2,064 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;
use Test::RequiresInternet 'test.wikipedia.org' => 80;
use Test::More 0.88;

use MediaWiki::Bot;
my $t = __FILE__;

my $host     = 'test.wikipedia.org';
my $username = $ENV{'PWPUsername'};
my $password = $ENV{'PWPPassword'};
plan skip_all => 'Login with patrol rights required'
    unless $host and $username and defined $password;

my $bot = MediaWiki::Bot->new({
    agent => "MediaWiki::Bot tests (https://metacpan.org/MediaWiki::Bot; $t)",
    login_data => {
        username => $username,
        password => $password,
        do_sul => 0,
    },
    host => $host,
    protocol => 'https',
});

my $tests_run = 0;
{
    my @rc = grep { defined $_->{rcid} and $_->{type} eq 'edit' }
        $bot->recentchanges(0, 5);

    foreach my $change (@rc) {
        my $success = $bot->patrol($change->{rcid});

        if ($bot->{error}->{details} and $bot->{error}->{details} =~ m/^(?:permissiondenied|badtoken)/) {
            pass q{Account isn't permitted to patrol};
            note explain $bot->{error};
            $tests_run++;
            last;
        }
        else {
            ok $success, 'Patrolled OK'
                or diag explain { res => $success, err => $bot->{error} };
            $tests_run++;
        }
    }
}

{
    my @rc = $bot->recentchanges(0, 5, { hook => \&mysub });

    sub mysub {
        my ($res) = @_;
        foreach my $hashref (@$res) {
            next unless defined $hashref->{rcid} and $hashref->{type} eq 'edit';
            my $success = $bot->patrol($hashref->{rcid});

            if ($bot->{error}->{details} and $bot->{error}->{details} =~ m/^(?:permissiondenied|badtoken)/) {
                pass q{Account isn't permitted to patrol};
                note explain $bot->{error};
                $tests_run++;
                last;
            }
            else {
                ok $success, 'Patrolled the page OK'
                    or diag explain { res => $res, err => $bot->{error} };
                $tests_run++;
            }
        }
    }
}

done_testing($tests_run);