--- /dev/null
+# -*-Perl-*-
+# Newslash::Model::Metamoderations related tests
+use Mojo::Base -strict;
+
+use Test::More;
+use Test::Mojo;
+use Data::Dumper;
+
+use POSIX qw(strftime);
+
+my $t = Test::Mojo->new('Newslash::Web');
+my $moderations = $t->app->model('moderations');
+my $metamoderations = $t->app->model('metamoderations');
+my $users = $t->app->model('users');
+my $comments = $t->app->model('comments');
+my $stories = $t->app->model('stories');
+
+use constant FAKE_IPID => "0123456789abcdef0123456789abcdef";
+use constant FAKE_SUBNETID => "0123456789abcdef0123456789abcdef";
+
+sub create_users {
+ my ($prefix, $users_count) = @_;
+
+ my $test_users = {};
+ for my $n (1..$users_count) {
+ my $name = $prefix . $n;
+ my $user = $users->select(nickname => $name);
+ if ($user) {
+ diag("user $user->{uid} ($user->{nickname}) already exists.");
+ }
+ else {
+ $user = $users->create($name, $name . '@example.com');
+ ok($user && $user->{error} == 0, "create user $name");
+ diag("$name: $user->{uid} - $user->{password}");
+ $user = $users->select(uid => $user->{uid});
+ }
+ $test_users->{$name} = $user;
+ ok($test_users->{$name}, "select user $name");
+ $test_users->{$name}->{ipid} = FAKE_IPID;
+ $test_users->{$name}->{subnetid} = FAKE_SUBNETID;
+ }
+
+ return $test_users;
+}
+
+sub create_root_user {
+ my $name = shift;
+
+ # root user
+ my $user = $users->select(nickname => $name);
+ if ($user) {
+ diag("user $user->{uid} ($user->{nickname}) already exists.");
+ }
+ else {
+ $user = $users->create($name, $name . '@example.com');
+ ok($user && $user->{error} == 0, "create roottest user");
+ diag("$name: $user->{uid} - $user->{password}");
+ }
+ my $rs = $users->update(uid => $user->{uid},
+ seclev => 10000,
+ author => 1);
+ ok($rs, "update modtest01 info");
+
+ my $admin = $users->select(uid => $user->{uid});
+ ok($admin, "select admin user");
+ $admin->{ipid} = FAKE_IPID;
+ $admin->{subnetid} = FAKE_SUBNETID;
+
+ return $admin;
+}
+
+sub create_story {
+ my ($admin, $title, $introtext) = @_;
+ my $story_params = {
+ title => $title,
+ introtext => $introtext,
+ topics_chosen => { 49 => 10 } , #mainpage, tid => weight style
+ uid => $admin->{uid},
+ time => strftime('%F %T', gmtime),
+ };
+ my $sid = $stories->create($story_params, $admin);
+ ok($sid, "story creation");
+ diag("sid: $sid");
+
+ my $story = $stories->select(sid => $sid);
+ ok($story, "story select");
+ return $story;
+}
+
+sub create_comments {
+ my ($discussion_id, $comms, $pid) = @_;
+ my $rs = {};
+ for my $c (@$comms) {
+ my $params = {
+ discussion_id => $discussion_id,
+ title => $c->{title},
+ comment => $c->{comment},
+ pid => $pid,
+ };
+ my $cid = $comments->create($params, $c->{author});
+ ok($cid, "comment creation $c->{key}");
+ if (!$cid) {
+ diag($comments->last_error);
+ }
+ diag("comment$c->{key} : $cid");
+ my $comment = $comments->select(cid => $cid);
+ ok($comment, "select comment $c->{key}");
+ $rs->{$c->{key}} = $comment;
+ if ($c->{children}) {
+ my $rs2 = create_comments($discussion_id, $c->{children}, $cid);
+
+ $rs = { %$rs, %$rs2 };
+ }
+ }
+ return $rs;
+}
+
+SKIP: {
+ skip "mode is not 'test'", 1 if ($t->app->mode ne 'test');
+ # add some parameter to emulate web access
+
+ # at first, create test users.
+ my $test_users = create_users("mmtest", 10);
+ my $admin = create_root_user("mmtest_admin");
+
+ # second, create story.
+ my $story = create_story($admin, "メタモデテスト", "<p>メタモデテスト本文</p>");
+ my $discussion_id = $story->{discussion};
+
+ my $anonymous = $users->anonymous_user;
+ ok($anonymous, "get anonymous user");
+ $anonymous->{ipid} = FAKE_IPID;
+ $anonymous->{subnetid} = FAKE_SUBNETID;
+
+ # create comments
+ my $comments = [
+ {
+ key => "01",
+ title => "コメントテスト01",
+ comment => "コメントテスト01の本文です。",
+ author => $anonymous,
+ children => [
+ {
+ key => "02",
+ title => "Re: コメントテスト01",
+ comment => "コメントテスト02の本文です。コメントテスト01の子コメントです。",
+ author => $test_users->{mmtest1},
+ },
+ ],
+ },
+ {
+ key => "03",
+ title => "コメントテスト03",
+ comment => "コメントテスト03の本文です。",
+ author => $test_users->{mmtest2},
+ children => [
+ {
+ key => "04",
+ title => "コメントテスト04",
+ comment => "コメントテスト04の本文です。コメントテスト03の子コメントです。",
+ author => $test_users->{mmtest3},
+ },
+ ],
+ },
+ {
+ key => "05",
+ title => "コメントテスト05",
+ comment => "コメントテスト05の本文です。",
+ author => $test_users->{mmtest4},
+ },
+ ];
+ my $test_comments = create_comments($discussion_id, $comments);
+ ok($test_comments, "create comments");
+ #diag(Dumper($test_comments));
+ # get moderation reasons
+ #my $reasons = $moderations->reasons;
+ #ok($reasons, "get reasons");
+
+ # moderation comment02
+ my $rs = $moderations->create($admin, $test_comments->{"02"}->{cid}, 8); # id 8 is おもしろおかしい
+ if (!$rs) {
+ diag($moderations->last_error);
+ }
+ ok($rs, "plus moderation");
+
+ # moderation comment03
+ if (!$rs) {
+ diag($moderations->last_error);
+ }
+ $rs = $moderations->create($admin, $test_comments->{"05"}->{cid}, 3); # id 3 is 荒し
+ ok($rs, "minus moderation");
+
+ # finished. do cleanup...
+ # delete test users
+ #for my $n (0..9) {
+ # my $name = "mmtest" . $n;
+ # my $rs = $users->hard_delete($test_users->{$name}->{uid});
+ # ok($rs, "delete user $name");
+ #}
+
+}
+
+sub check_diff {
+ my ($before, $after) = @_;
+ for my $k (keys %$before) {
+ if (!defined $before->{$k} || !defined $after->{$k}) {
+ if (defined $before->{$k}) {
+ diag("key $k is changed, $before->{$k} to undefined");
+ }
+ if (defined $after->{$k}) {
+ diag("key $k is changed, undefined to $after->{$k}");
+ }
+ next;
+ }
+ if ($before->{$k} ne $after->{$k}) {
+ diag("key $k is changed, $before->{$k} to $after->{$k}");
+ }
+ }
+}
+done_testing();