Trog-DataSource.t 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #!/usr/bin/env perl
  2. # A datasource's posts are built fresh on every view and have never been near
  3. # the datastore, so the search the reader typed -- which get() applied to the
  4. # datastore and then threw away along with the posts it filtered -- has to be
  5. # applied to them by somebody. This is that somebody.
  6. use 5.040;
  7. use warnings;
  8. use FindBin;
  9. use lib "$FindBin::Bin/../lib";
  10. use Test::More;
  11. use Test::Fatal qw{exception};
  12. require_ok('Trog::DataSource') or BAIL_OUT("Can't find SUT");
  13. my $t = 1700000000;
  14. sub guest {
  15. my ( $title, %o ) = @_;
  16. return {
  17. id => "id-$title",
  18. title => $title,
  19. data => '',
  20. created => $t++,
  21. user => 'bob',
  22. version => 0,
  23. %o,
  24. };
  25. }
  26. my @posts = (
  27. guest( 'webserver', state => 'running', hypervisor_title => 'Rack One' ),
  28. guest( 'database', state => 'shut off', hypervisor_title => 'Rack One' ),
  29. guest( 'mailhost', state => 'running', hypervisor_title => 'Rack Two', user => 'ann' ),
  30. );
  31. sub titles {
  32. my (@got) = @_;
  33. return [ sort map { $_->{title} } @got ];
  34. }
  35. subtest 'the default filter searches title and body' => sub {
  36. is_deeply( titles( Trog::DataSource::filter( {}, @posts ) ), titles(@posts), 'an empty query filters nothing' );
  37. is_deeply( titles( Trog::DataSource::filter( undef, @posts ) ), titles(@posts), 'and neither does no query at all' );
  38. is_deeply( titles( Trog::DataSource::filter( { like => 'web' }, @posts ) ), ['webserver'], 'a substring of the title matches' );
  39. is_deeply( titles( Trog::DataSource::filter( { like => 'SERVER' }, @posts ) ), ['webserver'], 'case insensitively' );
  40. is_deeply( titles( Trog::DataSource::filter( { like => 'erve' }, @posts ) ), ['webserver'], 'inside a word' );
  41. is_deeply( titles( Trog::DataSource::filter( { like => 'nonesuch' }, @posts ) ), [], 'and nothing matches nothing' );
  42. # The body counts too, for a source whose posts have one.
  43. my @bodied = ( guest( 'notes', data => 'the quick brown fox' ) );
  44. is_deeply( titles( Trog::DataSource::filter( { like => 'quick' }, @bodied ) ), ['notes'], 'the body is searched as well' );
  45. # The default does NOT know about a source's own fields -- that is what a
  46. # source implementing filter() is for.
  47. is_deeply( titles( Trog::DataSource::filter( { like => 'Rack One' }, @posts ) ), [], 'but a field it has never heard of is not' );
  48. };
  49. subtest 'the rest of the query still means what it means' => sub {
  50. is_deeply( titles( Trog::DataSource::filter( { author => 'ann' }, @posts ) ), ['mailhost'], 'author filters on the post user' );
  51. is_deeply( titles( Trog::DataSource::filter( { author => 'nobody' }, @posts ) ), [], 'and an author with nothing gets nothing' );
  52. is_deeply( titles( Trog::DataSource::filter( { older => $posts[1]{created} }, @posts ) ), ['webserver'], 'older is older' );
  53. is_deeply( titles( Trog::DataSource::filter( { newer => $posts[1]{created} }, @posts ) ), ['mailhost'], 'and newer is newer' );
  54. # Straight off a query string, so it is coerced rather than trusted.
  55. is_deeply(
  56. titles( Trog::DataSource::filter( { older => "$posts[1]{created}abc" }, @posts ) ),
  57. ['webserver'],
  58. 'a non-numeric older is coerced rather than dying'
  59. );
  60. # Combined, the way a search on a filtered page arrives.
  61. is_deeply( titles( Trog::DataSource::filter( { like => 'host', author => 'ann' }, @posts ) ), ['mailhost'], 'filters compose' );
  62. is_deeply( titles( Trog::DataSource::filter( { like => 'host', author => 'bob' }, @posts ) ), [], 'and disagree when they should' );
  63. };
  64. subtest 'what the default deliberately does not do' => sub {
  65. # Tags and acls are answers about the datastore, and they are settled before
  66. # a datasource is reached -- the reader got to the series page by holding
  67. # its acls, and every synthesized post belongs to that series. Applying
  68. # them again would blank the page for a source whose posts carry no tags,
  69. # which nothing requires them to.
  70. is_deeply( titles( Trog::DataSource::filter( { acls => [] }, @posts ) ), titles(@posts), 'an empty acl list does not blank the page' );
  71. is_deeply( titles( Trog::DataSource::filter( { tags => ['nonesuch'] }, @posts ) ), titles(@posts), 'nor does a tag nothing carries' );
  72. # An id query would otherwise match the series, not its children, and leave
  73. # nothing at all.
  74. is_deeply( titles( Trog::DataSource::filter( { id => 'the-series' }, @posts ) ), titles(@posts), 'and neither does the series id' );
  75. };
  76. subtest 'searchable_match' => sub {
  77. ok( Trog::DataSource::searchable_match( 'ell', 'hello' ), 'a substring matches' );
  78. ok( Trog::DataSource::searchable_match( 'ELL', 'hello' ), 'case insensitively' );
  79. ok( Trog::DataSource::searchable_match( 'x', 'a', undef, 'x' ), 'across several values' );
  80. ok( !Trog::DataSource::searchable_match( 'zz', 'a', 'b' ), 'and misses when it should' );
  81. ok( !Trog::DataSource::searchable_match( '', 'anything' ), 'an empty search matches nothing' );
  82. ok( !Trog::DataSource::searchable_match( undef, 'anything' ), 'and neither does no search' );
  83. # Quotemeta'd, so a search for punctuation is a search for punctuation.
  84. ok( !Trog::DataSource::searchable_match( '.*', 'anything' ), 'a regex is text, not a pattern' );
  85. ok( Trog::DataSource::searchable_match( '.*', 'a .* b' ), 'and matches where that text is' );
  86. is( exception { Trog::DataSource::searchable_match( '(unclosed', 'x' ) }, undef, 'even when it would not compile' );
  87. # A body stored as an array would otherwise be matched against its address.
  88. ok( !Trog::DataSource::searchable_match( 'ARRAY', ['slide one'] ), 'a reference is skipped, not stringified' );
  89. };
  90. subtest 'for_type' => sub {
  91. is( Trog::DataSource::for_type(), undef, 'no type names no source' );
  92. is( Trog::DataSource::for_type(''), undef, 'and neither does an empty one' );
  93. is( Trog::DataSource::for_type('blog.tx'), undef, 'a type with no datasource names none' );
  94. };
  95. subtest 'cacheable' => sub {
  96. # A datasource page is cached like any other unless something says not to,
  97. # and nothing else ever invalidates it: saving a post does not, because no
  98. # post was saved. A source that cannot say when its posts go stale would
  99. # therefore be cached forever wrong.
  100. is( Trog::DataSource::cacheable('Trog::DataSource::TestPlain'), 0, 'a source which says nothing is not cacheable' );
  101. is( Trog::DataSource::cacheable(undef), 0, 'and neither is no source at all' );
  102. require Trog::DataSource::Virt;
  103. is( Trog::DataSource::cacheable('Trog::DataSource::Virt'), 0, 'Virt is not: a guest\'s state is the point of the page' );
  104. require Trog::DataSource::DirIndex;
  105. is( Trog::DataSource::cacheable('Trog::DataSource::DirIndex'), 1, 'DirIndex is, because it watches the directory' );
  106. };
  107. subtest 'the route marks an uncacheable page nocache' => sub {
  108. require Trog::Routes::HTML;
  109. require Test::MockModule;
  110. my $meta = Test::MockModule->new('Trog::DataModule');
  111. my $source;
  112. $meta->redefine( type_meta_for => sub { return $source ? { 'x-tcms-datasource' => $source } : {} } );
  113. my $query = sub { return { primary_post => { child_form => 'fake.tx' } } };
  114. # An ordinary page is left alone.
  115. $source = undef;
  116. my $ordinary = $query->();
  117. Trog::Routes::HTML::_datasource_posts( $ordinary, [ guest('stored') ] );
  118. ok( !$ordinary->{nocache}, 'an ordinary page is not marked' );
  119. $source = 'Trog::DataSource::TestPlain';
  120. my $uncacheable = $query->();
  121. Trog::Routes::HTML::_datasource_posts( $uncacheable, [] );
  122. is( $uncacheable->{nocache}, 1, 'a source which has not said is marked nocache' );
  123. $source = 'Trog::DataSource::TestCached';
  124. my $cacheable = $query->();
  125. Trog::Routes::HTML::_datasource_posts( $cacheable, [] );
  126. ok( !$cacheable->{nocache}, 'and one which has said so is not' );
  127. };
  128. subtest 'nocache on the query reaches the renderer' => sub {
  129. require Trog::Renderer;
  130. # TCMS::build_routes puts a route's nocache flag on the query, and exactly
  131. # one caller has ever forwarded it to render() as an option. Everything
  132. # else was relying on being auth-gated, which says nothing about a route
  133. # that is nocache and public -- which is what an uncacheable datasource
  134. # page is.
  135. my @saved;
  136. my $tpsgi = bless {}, 'FakeTPSGI2';
  137. {
  138. no warnings qw{once};
  139. *FakeTPSGI2::add_post_close_callback = sub { $_[1]->(); return 1 };
  140. *FakeTPSGI2::save_render = sub { push( @saved, $_[1] ); return 1 };
  141. }
  142. # The dispatch table holds a code ref taken at load time, so redefining the
  143. # sub it came from leaves the table pointing at the original. Localise the
  144. # entry instead.
  145. no warnings qw{once};
  146. local $Trog::Renderer::renderers{html} = sub { return [ 200, [], ['a page'] ] };
  147. my %common = ( contenttype => 'text/html', template => 'whatever.tx', code => 200 );
  148. Trog::Renderer->render( %common, data => { route => '/cacheable', tpsgi => $tpsgi } );
  149. is_deeply( \@saved, ['/cacheable'], 'an ordinary page is saved as a static' );
  150. @saved = ();
  151. Trog::Renderer->render( %common, data => { route => '/live', tpsgi => $tpsgi, nocache => 1 } );
  152. is_deeply( \@saved, [], 'one whose query says nocache is not' );
  153. };
  154. subtest 'the defaults are advertised' => sub {
  155. ok( length Trog::DataSource::lang(), 'there is a language to advertise' );
  156. like( Trog::DataSource::help(), qr{^https?://}, 'and somewhere to read about it' );
  157. };
  158. subtest 'Trog::DataSource::Virt searches what its posts carry' => sub {
  159. require_ok('Trog::DataSource::Virt') or return;
  160. # A guest post's body is empty; everything about it is in named fields.
  161. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'web' }, @posts ) ), ['webserver'], 'the guest name is searched' );
  162. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'shut' }, @posts ) ), ['database'], 'and so is its state' );
  163. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'Rack One' }, @posts ) ), [ sort qw{database webserver} ], 'and the hypervisor it is on' );
  164. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'nonesuch' }, @posts ) ), [], 'and nothing matches nothing' );
  165. # The rest of the query is still the default's job.
  166. is_deeply( titles( Trog::DataSource::Virt::filter( { author => 'ann' }, @posts ) ), ['mailhost'], 'author still filters' );
  167. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'running', author => 'ann' }, @posts ) ), ['mailhost'], 'alongside the search' );
  168. # It says so, rather than letting the page advertise the data model's.
  169. isnt( Trog::DataSource::Virt::lang(), Trog::DataSource::lang(), 'and it describes its own search' );
  170. like( Trog::DataSource::Virt::lang(), qr/guest/i, 'in terms of what is on the page' );
  171. };
  172. #--------------------------------------------------------------------------
  173. # The wiring: a route has to actually reach for all this.
  174. #--------------------------------------------------------------------------
  175. BEGIN {
  176. $INC{'Trog/DataSource/TestPlain.pm'} = 1;
  177. $INC{'Trog/DataSource/TestPicky.pm'} = 1;
  178. $INC{'Trog/DataSource/TestBroken.pm'} = 1;
  179. $INC{'Trog/DataSource/TestCached.pm'} = 1;
  180. }
  181. {
  182. # A source with no filter() of its own: the default has to be applied for it.
  183. package Trog::DataSource::TestPlain;
  184. sub posts { return ( main::guest('alpha'), main::guest('beta') ) }
  185. # One which can say when its posts go stale, and so may be cached.
  186. package Trog::DataSource::TestCached;
  187. use constant CACHEABLE => 1;
  188. sub posts { return ( main::guest('cached') ) }
  189. # One with opinions, including its own search language.
  190. package Trog::DataSource::TestPicky;
  191. our @FILTERED;
  192. sub posts { return ( main::guest('alpha'), main::guest('beta') ) }
  193. sub filter {
  194. my ( $q, @p ) = @_;
  195. push @FILTERED, $q->{like};
  196. return grep { $_->{title} eq 'beta' } @p;
  197. }
  198. sub lang { 'Picky search' }
  199. sub help { 'https://example.com/picky' }
  200. # One whose filter blows up.
  201. package Trog::DataSource::TestBroken;
  202. sub posts { return ( main::guest('alpha') ) }
  203. sub filter { die "no\n" }
  204. }
  205. subtest 'the route applies the search to synthesized posts' => sub {
  206. require Trog::Routes::HTML;
  207. require Test::MockModule;
  208. my $meta = Test::MockModule->new('Trog::DataModule');
  209. my $source;
  210. $meta->redefine( type_meta_for => sub { return $source ? { 'x-tcms-datasource' => $source } : {} } );
  211. my $query = sub { return { primary_post => { child_form => 'fake.tx' }, @_ } };
  212. # No datasource at all: the stored posts are handed straight back.
  213. $source = undef;
  214. my @stored = ( guest('stored') );
  215. is_deeply( titles( Trog::Routes::HTML::_datasource_posts( $query->(), \@stored ) ), ['stored'], 'an ordinary type is left alone' );
  216. $source = 'Trog::DataSource::TestPlain';
  217. is_deeply( titles( Trog::Routes::HTML::_datasource_posts( $query->(), \@stored ) ), [ sort qw{alpha beta} ], 'a datasource replaces them' );
  218. # The point of the exercise.
  219. is_deeply(
  220. titles( Trog::Routes::HTML::_datasource_posts( $query->( like => 'alph' ), \@stored ) ),
  221. ['alpha'],
  222. 'and the search reaches them, rather than being dropped'
  223. );
  224. is_deeply(
  225. titles( Trog::Routes::HTML::_datasource_posts( $query->( like => 'nonesuch' ), \@stored ) ),
  226. [],
  227. 'including when it matches nothing'
  228. );
  229. # A source with its own filter gets to use it.
  230. $source = 'Trog::DataSource::TestPicky';
  231. @Trog::DataSource::TestPicky::FILTERED = ();
  232. is_deeply(
  233. titles( Trog::Routes::HTML::_datasource_posts( $query->( like => 'alph' ), \@stored ) ),
  234. ['beta'],
  235. "a source's own filter wins over the default"
  236. );
  237. is_deeply( \@Trog::DataSource::TestPicky::FILTERED, ['alph'], 'and it is handed the query' );
  238. # A filter that dies must not take the page with it.
  239. $source = 'Trog::DataSource::TestBroken';
  240. my @survived;
  241. {
  242. my $log = Test::MockModule->new('Trog::Routes::HTML');
  243. $log->redefine( WARN => sub { note(shift) } );
  244. @survived = Trog::Routes::HTML::_datasource_posts( $query->( like => 'x' ), \@stored );
  245. }
  246. is_deeply( titles(@survived), ['alpha'], 'a filter that dies falls back to the unfiltered posts' );
  247. # A sidecar cannot make us load whatever it likes.
  248. $source = 'Evil::Module';
  249. my @refused;
  250. {
  251. my $log = Test::MockModule->new('Trog::DataSource');
  252. $log->redefine( WARN => sub { note(shift) } );
  253. @refused = Trog::Routes::HTML::_datasource_posts( $query->(), \@stored );
  254. }
  255. is_deeply( titles(@refused), ['stored'], 'a datasource outside the namespace is refused' );
  256. };
  257. subtest 'the search box says what it is searching' => sub {
  258. require Trog::Routes::HTML;
  259. require Test::MockModule;
  260. # Stubbed rather than built: constructing a real model here would create a
  261. # datastore in the repo, and none of this is about which model is configured.
  262. my $data = Test::MockModule->new('Trog::Data');
  263. $data->redefine( new => sub { return bless {}, 'FakeModel' } );
  264. {
  265. no warnings qw{once};
  266. *FakeModel::lang = sub { 'Model language' };
  267. *FakeModel::help = sub { 'https://example.com/model' };
  268. }
  269. my $meta = Test::MockModule->new('Trog::DataModule');
  270. my $source;
  271. $meta->redefine( type_meta_for => sub { return $source ? { 'x-tcms-datasource' => $source } : {} } );
  272. $source = undef;
  273. is_deeply(
  274. [ Trog::Routes::HTML::_search_language( { primary_post => { child_form => 'blog.tx' } } ) ],
  275. [ 'Model language', 'https://example.com/model' ],
  276. 'an ordinary page advertises the data model'
  277. );
  278. is_deeply(
  279. [ Trog::Routes::HTML::_search_language( {} ) ],
  280. [ 'Model language', 'https://example.com/model' ],
  281. 'and so does a page with no series at all'
  282. );
  283. # A datasource page is not being served out of the datastore, so the model's
  284. # query language is the wrong thing to advertise on it.
  285. $source = 'Trog::DataSource::TestPlain';
  286. is_deeply(
  287. [ Trog::Routes::HTML::_search_language( { primary_post => { child_form => 'fake.tx' } } ) ],
  288. [ Trog::DataSource::lang(), Trog::DataSource::help() ],
  289. 'a datasource page advertises the datasource default'
  290. );
  291. $source = 'Trog::DataSource::TestPicky';
  292. is_deeply(
  293. [ Trog::Routes::HTML::_search_language( { primary_post => { child_form => 'fake.tx' } } ) ],
  294. [ 'Picky search', 'https://example.com/picky' ],
  295. 'or its own, when it has one'
  296. );
  297. };
  298. subtest 'ordering is total, so page 2 means something' => sub {
  299. my @same = (
  300. guest( 'charlie', created => 100 ),
  301. guest( 'alpha', created => 100 ),
  302. guest( 'bravo', created => 100 ),
  303. guest( 'delta', created => 200 ),
  304. );
  305. my @ordered = Trog::DataSource::order( {}, @same );
  306. is_deeply( [ map { $_->{title} } @ordered ], [qw{delta alpha bravo charlie}], 'newest first, then by title' );
  307. # The property that actually matters: same input, same order, every time.
  308. is_deeply(
  309. [ map { $_->{title} } Trog::DataSource::order( {}, reverse @same ) ],
  310. [ map { $_->{title} } @ordered ],
  311. 'and it does not depend on what order they arrived in'
  312. );
  313. # A source that stamps everything with the time it built them -- which is
  314. # every libvirt guest -- falls back to being sorted by name.
  315. my @built = map { guest( $_, created => 1700 ) } qw{zulu alpha mike};
  316. is_deeply( [ map { $_->{title} } Trog::DataSource::order( {}, @built ) ], [qw{alpha mike zulu}], 'identical timestamps sort by name' );
  317. # Missing fields must not make it die or shuffle.
  318. my @sparse = ( { id => 'b' }, { id => 'a' }, guest('titled') );
  319. is( exception { Trog::DataSource::order( {}, @sparse ) }, undef, 'posts missing created and title are still orderable' );
  320. };
  321. subtest 'offset pagination' => sub {
  322. require Trog::Routes::HTML;
  323. my @ten = map { guest( sprintf( 'g%02d', $_ ), created => 100 + $_ ) } 1 .. 10;
  324. my ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 1 }, 4, @ten );
  325. is( $page, 1, 'page one is page one' );
  326. is( $total, 3, 'ten posts at four a page is three pages' );
  327. is( scalar(@slice), 4, 'and a full page' );
  328. is( $slice[0]{title}, 'g01', 'starting at the beginning' );
  329. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 2 }, 4, @ten );
  330. is( $slice[0]{title}, 'g05', 'page two carries on where page one stopped' );
  331. is( scalar(@slice), 4, 'and is also full' );
  332. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 3 }, 4, @ten );
  333. is( scalar(@slice), 2, 'the last page holds the remainder' );
  334. is( $slice[0]{title}, 'g09', 'from where page two stopped' );
  335. # Every post appears exactly once across the pages, which is the whole point.
  336. my @walked;
  337. push( @walked, ( Trog::Routes::HTML::_paginate_offset( { page => $_ }, 4, @ten ) )[ 2 .. 5 ] ) for 1 .. 3;
  338. @walked = grep { defined } @walked;
  339. is_deeply( [ sort map { $_->{title} } @walked ], [ sort map { $_->{title} } @ten ], 'walking the pages visits everything, once' );
  340. # Straight off a query string.
  341. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 99 }, 4, @ten );
  342. is( $page, 3, 'a page past the end shows the last one' );
  343. ok( scalar(@slice), 'rather than nothing at all' );
  344. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 0 }, 4, @ten );
  345. is( $page, 1, 'and page zero is page one' );
  346. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 'nonsense' }, 4, @ten );
  347. is( $page, 1, 'as is a page that is not a number' );
  348. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( {}, 4, @ten );
  349. is( $page, 1, 'and no page at all' );
  350. # A limit that would divide by zero.
  351. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 1 }, 0, @ten );
  352. is( $total, 1, 'a limit of zero falls back to the default rather than dividing by it' );
  353. is( scalar(@slice), 10, 'and shows everything' );
  354. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 1 }, 4 );
  355. is( $total, 1, 'no posts is one empty page' );
  356. is( scalar(@slice), 0, 'holding nothing' );
  357. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 1 }, 25, @ten );
  358. is( $total, 1, 'fewer posts than a page is one page' );
  359. is( scalar(@slice), 10, 'holding all of them' );
  360. };
  361. subtest 'the paginator renders page links for a datasource' => sub {
  362. require Trog::Renderer::Base;
  363. my $render = sub {
  364. my (%vars) = @_;
  365. return Trog::Renderer::Base::render(
  366. template => 'paginator.tx',
  367. contenttype => 'text/html',
  368. component => 1,
  369. data => { limit => 25, sizes => [ 25, 50, 100 ], years => [2024], months => [ 0 .. 11 ], older => 0, newer => 0, %vars },
  370. );
  371. };
  372. my $offset = $render->( paginate_offset => 1, pages => 1, page => 2, pages_total => 5, like => 'web' );
  373. like( $offset, qr/Page 2 of 5/, 'it says where you are' );
  374. like( $offset, qr/href="\?page=1&limit=25/, 'Prev goes back a page' );
  375. like( $offset, qr/href="\?page=3&limit=25/, 'Next goes on a page' );
  376. # & because Xslate escapes what an expression emits, which is what an
  377. # ampersand in an attribute is supposed to be. The cursor branch is the same.
  378. like( $offset, qr/like=web/, 'and the search survives the trip' );
  379. unlike( $offset, qr/older=|newer=/, 'no cursor, which could not move on such a page anyway' );
  380. unlike( $offset, qr/Jump to/, 'and no date jump, since these posts share a date' );
  381. my $first = $render->( paginate_offset => 1, pages => 1, page => 1, pages_total => 3 );
  382. unlike( $first, qr/rel="prev"/, 'the first page offers no Prev' );
  383. like( $first, qr/rel="next"/, 'but does offer Next' );
  384. my $last = $render->( paginate_offset => 1, pages => 1, page => 3, pages_total => 3 );
  385. like( $last, qr/rel="prev"/, 'the last page offers Prev' );
  386. unlike( $last, qr/rel="next"/, 'and no Next' );
  387. my $only = $render->( paginate_offset => 1, pages => 0, page => 1, pages_total => 1 );
  388. unlike( $only, qr/rel="prev"|rel="next"/, 'a single page offers neither' );
  389. like( $only, qr/id="paginator" class="disabled"/, 'and says so' );
  390. # Stored posts are untouched by any of this.
  391. my $cursor = $render->( paginate_offset => 0, pages => 1, older => 1700, newer => 1800, like => 'x' );
  392. like( $cursor, qr/href="\?older=1700&limit=25/, 'the cursor paginator still pages by cursor' );
  393. like( $cursor, qr/href="\?newer=1800&limit=25/, 'both ways' );
  394. like( $cursor, qr/Jump to/, 'and keeps its date jump' );
  395. unlike( $cursor, qr/\?page=/, 'with no page numbers' );
  396. };
  397. done_testing();