fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-02-03 04:48 pm

[dw-free] new feeds pulling in entries in reverse order

[commit: http://hg.dwscoalition.org/dw-free/rev/da3157435c02]

http://bugs.dwscoalition.org/show_bug.cgi?id=2623

Add tests for $u->watch_items and $u->recent_items to test some basic
ordering.

Patch by [personal profile] fu.

Files modified:
  • t/log-items.t
--------------------------------------------------------------------------------
diff -r c79d1dcc92a6 -r da3157435c02 t/log-items.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/log-items.t	Fri Feb 04 00:48:19 2011 +0800
@@ -0,0 +1,117 @@
+#!/usr/bin/perl
+
+use strict;
+use Test::More tests => 5;
+use lib "$ENV{LJHOME}/cgi-bin";
+require 'ljlib.pl';
+
+use LJ::Test qw ( temp_user );
+use LJ::Entry;
+
+sub post_entry {
+    my ( $u, $entries ) = @_;
+    my $entry = $u->t_post_fake_entry( body => "test post " . time() . rand() );
+
+    unshift @{ $entries->{$u->id} }, $entry;
+    unshift @{ $entries->{all} }, $entry;
+}
+
+sub extract {
+    my ( $items ) = @_;
+
+    my @keys = qw( posterid jitemid );
+    my @ret;
+
+    foreach my $item ( @{ $items || {} } ) {
+        # extract the important attributes for comparison
+        my $entry = {};
+        $entry->{$_} = $item->{$_} foreach @keys;
+        push @ret, $entry;
+    }
+
+    return \@ret;
+}
+
+note( " ### RECENT ITEMS ### ");
+{
+    my $u = temp_user();
+    my $entries = {};
+
+    post_entry( $u, $entries );
+    post_entry( $u, $entries );
+    post_entry( $u, $entries );
+
+    my @entrylist = @{ $entries->{$u->id} || {} };
+    LJ::Protocol::do_request( "editevent", {
+        itemid      => $entrylist[1]->jitemid,
+        ver         => 1,
+        username    => $u->user,
+        year        => "1999",  # arbitary early date
+    }, undef, {
+        noauth          => 1,
+        use_old_content => 1,
+    } );
+
+    LJ::Entry->reset_singletons;
+    $entrylist[1] = LJ::Entry->new( $u, jitemid => $entrylist[1]->jitemid );
+
+
+    # fragile because we can't control the system log time
+    # so instead, let's just skip this test if the generated entries
+    # don't have a consistent logtime
+    SKIP: {
+        my $logtime = $entrylist[0]->logtime_unix;
+        my $do_skip = 0;
+        foreach ( @entrylist ) {
+            $do_skip = 1 if $_->logtime_unix != $logtime;
+        }
+
+        skip "Test is fragile so we skip if the logtime isn't consistent among the newly-posted entries", 2 if $do_skip;
+
+        # in user-visible display order
+        # i.e., for personal journals
+        my @recent_items = $u->recent_items(
+            clusterid     => $u->{clusterid},
+            clustersource => 'slave',
+            itemshow      => 3,
+        );
+        is_deeply( [ map { $_->{itemid} } @recent_items ], [ 3, 1, 2 ], "Got entries back, ordered by the user-specified date." );
+
+        # in system time order
+        # i.e., communities and feeds
+        @recent_items = $u->recent_items(
+            clusterid     => $u->{clusterid},
+            clustersource => 'slave',
+            itemshow      => 3,
+            order         => "logtime",
+        );
+        is_deeply( [ map { $_->{itemid} } @recent_items ], [ 3, 2, 1 ], "Got entries back, ordered by the system-recorded date." );
+
+    }
+}
+
+
+note( " ### WATCH ITEMS ### " );
+note( "basic merging of watched items from different users, no security" );
+{
+    my $u = temp_user();
+
+    my $w1 = temp_user();
+    my $w2 = temp_user();
+    my $entries = {};
+
+    post_entry( $w1, $entries ); sleep( 1 );
+    post_entry( $w2, $entries ); sleep( 1 );
+    post_entry( $w1, $entries );
+
+    my @watch_items = $u->watch_items( itemshow => 3 );
+    is_deeply( \@watch_items, [], "No watch items" );
+
+    $u->add_edge( $w1, watch => {} );
+    @watch_items = $u->watch_items( itemshow => 3 );
+    is_deeply(  extract( \@watch_items ), extract( $entries->{$w1->id} ), "Items from \$w1" );
+
+    $u->add_edge( $w2, watch => {} );
+    @watch_items = $u->watch_items( itemshow => 3 );
+   is_deeply( extract( \@watch_items ), extract( $entries->{all} ), "Items from \$w1 and \$w2" );
+}
--------------------------------------------------------------------------------