Trog-DataSource.t 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 strict;
  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 'the defaults are advertised' => sub {
  96. ok( length Trog::DataSource::lang(), 'there is a language to advertise' );
  97. like( Trog::DataSource::help(), qr{^https?://}, 'and somewhere to read about it' );
  98. };
  99. subtest 'Trog::DataSource::Virt searches what its posts carry' => sub {
  100. require_ok('Trog::DataSource::Virt') or return;
  101. # A guest post's body is empty; everything about it is in named fields.
  102. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'web' }, @posts ) ), ['webserver'], 'the guest name is searched' );
  103. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'shut' }, @posts ) ), ['database'], 'and so is its state' );
  104. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'Rack One' }, @posts ) ), [ sort qw{database webserver} ], 'and the hypervisor it is on' );
  105. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'nonesuch' }, @posts ) ), [], 'and nothing matches nothing' );
  106. # The rest of the query is still the default's job.
  107. is_deeply( titles( Trog::DataSource::Virt::filter( { author => 'ann' }, @posts ) ), ['mailhost'], 'author still filters' );
  108. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'running', author => 'ann' }, @posts ) ), ['mailhost'], 'alongside the search' );
  109. # It says so, rather than letting the page advertise the data model's.
  110. isnt( Trog::DataSource::Virt::lang(), Trog::DataSource::lang(), 'and it describes its own search' );
  111. like( Trog::DataSource::Virt::lang(), qr/guest/i, 'in terms of what is on the page' );
  112. };
  113. #--------------------------------------------------------------------------
  114. # The wiring: a route has to actually reach for all this.
  115. #--------------------------------------------------------------------------
  116. BEGIN {
  117. $INC{'Trog/DataSource/TestPlain.pm'} = 1;
  118. $INC{'Trog/DataSource/TestPicky.pm'} = 1;
  119. $INC{'Trog/DataSource/TestBroken.pm'} = 1;
  120. }
  121. {
  122. # A source with no filter() of its own: the default has to be applied for it.
  123. package Trog::DataSource::TestPlain;
  124. sub posts { return ( main::guest('alpha'), main::guest('beta') ) }
  125. # One with opinions, including its own search language.
  126. package Trog::DataSource::TestPicky;
  127. our @FILTERED;
  128. sub posts { return ( main::guest('alpha'), main::guest('beta') ) }
  129. sub filter {
  130. my ( $q, @p ) = @_;
  131. push @FILTERED, $q->{like};
  132. return grep { $_->{title} eq 'beta' } @p;
  133. }
  134. sub lang { 'Picky search' }
  135. sub help { 'https://example.com/picky' }
  136. # One whose filter blows up.
  137. package Trog::DataSource::TestBroken;
  138. sub posts { return ( main::guest('alpha') ) }
  139. sub filter { die "no\n" }
  140. }
  141. subtest 'the route applies the search to synthesized posts' => sub {
  142. require Trog::Routes::HTML;
  143. require Test::MockModule;
  144. my $meta = Test::MockModule->new('Trog::DataModule');
  145. my $source;
  146. $meta->redefine( type_meta_for => sub { return $source ? { 'x-tcms-datasource' => $source } : {} } );
  147. my $query = sub { return { primary_post => { child_form => 'fake.tx' }, @_ } };
  148. # No datasource at all: the stored posts are handed straight back.
  149. $source = undef;
  150. my @stored = ( guest('stored') );
  151. is_deeply( titles( Trog::Routes::HTML::_datasource_posts( $query->(), \@stored ) ), ['stored'], 'an ordinary type is left alone' );
  152. $source = 'Trog::DataSource::TestPlain';
  153. is_deeply( titles( Trog::Routes::HTML::_datasource_posts( $query->(), \@stored ) ), [ sort qw{alpha beta} ], 'a datasource replaces them' );
  154. # The point of the exercise.
  155. is_deeply(
  156. titles( Trog::Routes::HTML::_datasource_posts( $query->( like => 'alph' ), \@stored ) ),
  157. ['alpha'],
  158. 'and the search reaches them, rather than being dropped'
  159. );
  160. is_deeply(
  161. titles( Trog::Routes::HTML::_datasource_posts( $query->( like => 'nonesuch' ), \@stored ) ),
  162. [],
  163. 'including when it matches nothing'
  164. );
  165. # A source with its own filter gets to use it.
  166. $source = 'Trog::DataSource::TestPicky';
  167. @Trog::DataSource::TestPicky::FILTERED = ();
  168. is_deeply(
  169. titles( Trog::Routes::HTML::_datasource_posts( $query->( like => 'alph' ), \@stored ) ),
  170. ['beta'],
  171. "a source's own filter wins over the default"
  172. );
  173. is_deeply( \@Trog::DataSource::TestPicky::FILTERED, ['alph'], 'and it is handed the query' );
  174. # A filter that dies must not take the page with it.
  175. $source = 'Trog::DataSource::TestBroken';
  176. my @survived;
  177. {
  178. my $log = Test::MockModule->new('Trog::Routes::HTML');
  179. $log->redefine( WARN => sub { note(shift) } );
  180. @survived = Trog::Routes::HTML::_datasource_posts( $query->( like => 'x' ), \@stored );
  181. }
  182. is_deeply( titles(@survived), ['alpha'], 'a filter that dies falls back to the unfiltered posts' );
  183. # A sidecar cannot make us load whatever it likes.
  184. $source = 'Evil::Module';
  185. my @refused;
  186. {
  187. my $log = Test::MockModule->new('Trog::DataSource');
  188. $log->redefine( WARN => sub { note(shift) } );
  189. @refused = Trog::Routes::HTML::_datasource_posts( $query->(), \@stored );
  190. }
  191. is_deeply( titles(@refused), ['stored'], 'a datasource outside the namespace is refused' );
  192. };
  193. subtest 'the search box says what it is searching' => sub {
  194. require Trog::Routes::HTML;
  195. require Test::MockModule;
  196. # Stubbed rather than built: constructing a real model here would create a
  197. # datastore in the repo, and none of this is about which model is configured.
  198. my $data = Test::MockModule->new('Trog::Data');
  199. $data->redefine( new => sub { return bless {}, 'FakeModel' } );
  200. {
  201. no warnings qw{once};
  202. *FakeModel::lang = sub { 'Model language' };
  203. *FakeModel::help = sub { 'https://example.com/model' };
  204. }
  205. my $meta = Test::MockModule->new('Trog::DataModule');
  206. my $source;
  207. $meta->redefine( type_meta_for => sub { return $source ? { 'x-tcms-datasource' => $source } : {} } );
  208. $source = undef;
  209. is_deeply(
  210. [ Trog::Routes::HTML::_search_language( { primary_post => { child_form => 'blog.tx' } } ) ],
  211. [ 'Model language', 'https://example.com/model' ],
  212. 'an ordinary page advertises the data model'
  213. );
  214. is_deeply(
  215. [ Trog::Routes::HTML::_search_language( {} ) ],
  216. [ 'Model language', 'https://example.com/model' ],
  217. 'and so does a page with no series at all'
  218. );
  219. # A datasource page is not being served out of the datastore, so the model's
  220. # query language is the wrong thing to advertise on it.
  221. $source = 'Trog::DataSource::TestPlain';
  222. is_deeply(
  223. [ Trog::Routes::HTML::_search_language( { primary_post => { child_form => 'fake.tx' } } ) ],
  224. [ Trog::DataSource::lang(), Trog::DataSource::help() ],
  225. 'a datasource page advertises the datasource default'
  226. );
  227. $source = 'Trog::DataSource::TestPicky';
  228. is_deeply(
  229. [ Trog::Routes::HTML::_search_language( { primary_post => { child_form => 'fake.tx' } } ) ],
  230. [ 'Picky search', 'https://example.com/picky' ],
  231. 'or its own, when it has one'
  232. );
  233. };
  234. done_testing();