Преглед на файлове

Merge pull request #357 from Troglodyne-Internet-Widgets/every-alias-routes

Route every alias of a post, not only its first
George S. Baugh преди 1 седмица
родител
ревизия
5f5af6b78b
променени са 2 файла, в които са добавени 22 реда и са изтрити 1 реда
  1. 1 1
      lib/Trog/DataModule.pm
  2. 21 0
      t/Trog-Data-SQLite.t

+ 1 - 1
lib/Trog/DataModule.pm

@@ -1015,7 +1015,7 @@ sub aliases ($self) {
     my @posts = $self->get( limit => 0, acls => ['admin'] );
     my %aliases;
     foreach my $post (@posts) {
-        @aliases{ @{ $post->{aliases} } } = $post->{local_href};
+        $aliases{$_} = $post->{local_href} for @{ $post->{aliases} // [] };
     }
     return %aliases;
 }

+ 21 - 0
t/Trog-Data-SQLite.t

@@ -489,4 +489,25 @@ subtest 'indexing a custom field' => sub {
     ok( !$indexes->()->{'posts_custom_post_data'}, 'and a schema column did not pick up an index of its own' );
 };
 
+# Last, so the post it adds changes no count asserted above.
+subtest 'aliases' => sub {
+    my $aliased = post(
+        id         => 'p-aliased',
+        title      => 'Many Names',
+        local_href => '/posts/p-aliased',
+        method     => 'GET',
+        callback   => 'Trog::Routes::HTML::posts',
+        aliases    => [qw{/one /two /three}],
+    );
+    $sqlite->write( [ { %$aliased, tags => [ @{ $aliased->{tags} } ] } ] );
+    $flat->write( [ { %$aliased, tags => [ @{ $aliased->{tags} } ] } ] );
+
+    my %want = map { $_ => '/posts/p-aliased' } qw{/one /two /three};
+    foreach my $case ( [ flatfile => $flat ], [ sqlite => $sqlite ] ) {
+        my ( $name, $model ) = @$case;
+        my %got = $model->aliases();
+        is_deeply( { map { $_ => $got{$_} } grep { exists $want{$_} } keys %got }, \%want, "$name: every alias of a post leads to it, not only the first" );
+    }
+};
+
 done_testing();