Trog-Routes-HTML-series-lifecycle.t 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. #!/usr/bin/env perl
  2. # Stands up a genuinely fresh tCMS in a temp dir, then walks the loop a user
  3. # actually performs: register an admin, invent a post type in the wizard, and
  4. # for each content post type build a topbar series, confirm it lands in the
  5. # nav, add a child post and confirm the child renders on the series page.
  6. #
  7. # Verification goes through Trog::Routes::HTML::index() -- directly, or via
  8. # series() which delegates to it -- and matches on the returned HTML.
  9. use strict;
  10. use warnings;
  11. # FindBin MUST come first. $FindBin::Bin is derived from $0, which prove hands
  12. # us relatively, and it is computed once. chdir before this loads and every
  13. # module's `use FindBin::libs` resolves against the sandbox instead of the repo.
  14. use FindBin;
  15. use lib "$FindBin::Bin/../lib";
  16. use Test::More;
  17. use Cwd ();
  18. use Encode ();
  19. use File::Copy ();
  20. use File::Find ();
  21. use File::Path ();
  22. use File::Temp ();
  23. use Time::HiRes ();
  24. use URI::Escape ();
  25. use JSON::MaybeXS ();
  26. use File::Slurper ();
  27. our ( $REPO, $ROOT, $OLDCWD );
  28. our %REPO_BEFORE;
  29. # Which data model this run exercises. Every assertion in this file is about
  30. # what the app does, not how it stores anything, so all of them have to hold for
  31. # any of them.
  32. our $MODEL;
  33. # In BEGIN because the sandbox is built in one, before file scope runs.
  34. BEGIN { $MODEL = $ENV{TCMS_TEST_DATA_MODEL} || 'FlatFile' }
  35. # name => (size, mtime) for every file under the repo dirs this test could
  36. # plausibly disturb. Cheap enough, and it is the only real proof of isolation.
  37. sub _snapshot {
  38. my ($dir) = @_;
  39. my %seen;
  40. File::Find::find(
  41. {
  42. no_chdir => 1,
  43. wanted => sub {
  44. return unless -f $File::Find::name;
  45. my @st = stat(_);
  46. $seen{$File::Find::name} = "$st[7]:$st[9]";
  47. },
  48. },
  49. $dir
  50. );
  51. return %seen;
  52. }
  53. sub _copy_tree {
  54. my ( $from, $to ) = @_;
  55. File::Path::make_path($to);
  56. File::Find::find(
  57. {
  58. no_chdir => 1,
  59. wanted => sub {
  60. my $rel = substr( $File::Find::name, length($from) );
  61. return unless length $rel;
  62. my $dst = "$to$rel";
  63. if ( -d $File::Find::name ) { File::Path::make_path($dst) }
  64. elsif ( -f $File::Find::name ) { File::Copy::copy( $File::Find::name, $dst ) or die "copy $File::Find::name: $!" }
  65. },
  66. },
  67. $from
  68. );
  69. return;
  70. }
  71. # Everything the sandbox needs, in one BEGIN. The Trog:: modules are require()d
  72. # at runtime below, which is unconditionally after every BEGIN -- so no later
  73. # edit can accidentally load them before the chdir. That matters: TCMS.pm runs
  74. # build_routes() at file scope and FlatFile.pm reads the tag index at file
  75. # scope, both against whatever cwd happens to be current.
  76. BEGIN {
  77. $REPO = Cwd::abs_path("$FindBin::Bin/..");
  78. $OLDCWD = Cwd::getcwd();
  79. %REPO_BEFORE = map { _snapshot("$REPO/$_") } qw{config data www/templates www/assets logs totp};
  80. $ROOT = File::Temp::tempdir( 'tcms-lifecycle-XXXXXX', TMPDIR => 1, CLEANUP => 0 );
  81. # Installer.mk's tree, plus upload-src for the file.tx fixture.
  82. File::Path::make_path(
  83. map { "$ROOT/$_" }
  84. qw{
  85. config schema data/files logs totp upload-src
  86. www/assets/private www/statics www/themes www/scripts
  87. }
  88. );
  89. # A real fresh install: get() looks for config/main.cfg, doesn't find one,
  90. # and falls through to the shipped default. The data model is the shipped
  91. # one unless the environment names another, which is how this whole file
  92. # gets run against each of them -- the point being that a data model is only
  93. # finished when the app cannot tell which one it is talking to.
  94. my $stock = File::Slurper::read_text("$REPO/config/default.cfg");
  95. $stock =~ s/^(\s*data_model=).*$/$1$MODEL/m or die "could not set the data model in default.cfg";
  96. File::Slurper::write_text( "$ROOT/config/default.cfg", $stock );
  97. # Hardcoded paths in Auth.pm, TagIndex.pm, the data models and Log.pm -- not negotiable.
  98. File::Copy::copy( "$REPO/schema/$_", "$ROOT/schema/$_" ) or die $! for qw{auth.schema flatfile.schema sqlite.schema log.schema};
  99. # COPY, don't symlink: post_wizard_save() writes into Trog::Themes::forms_dir(),
  100. # and a symlink would land the generated post type in the real repo.
  101. _copy_tree( "$REPO/www/templates", "$ROOT/www/templates" );
  102. # posts.tx pulls in the EmojiPicker component for an editor, and EmojiPicker
  103. # dies outright if this file is missing. Only category and emoji are read.
  104. open( my $fh, '>', "$ROOT/www/scripts/list.min.json" ) or die $!;
  105. print {$fh} '{"emojis":[{"category":"Smileys","emoji":"X"}]}';
  106. close $fh;
  107. # Text::Xslate computes its default cache dir from $HOME once, at load time.
  108. # Without this the sandbox writes compiled templates into the real ~/.xslate_cache.
  109. $ENV{HOME} = $ROOT;
  110. chdir($ROOT) or die "could not chdir to sandbox $ROOT: $!";
  111. }
  112. END {
  113. # Never rmtree the directory you are standing in.
  114. chdir($OLDCWD) if $OLDCWD;
  115. if ( $ENV{TCMS_TEST_KEEP} ) {
  116. diag("TCMS_TEST_KEEP set, sandbox left at $ROOT");
  117. }
  118. elsif ( $ROOT && -d $ROOT ) {
  119. # SQLite handles are file-lexical in Trog::SQLite and unreachable from
  120. # here; DBI disconnects at global destruction, after these are gone.
  121. eval { File::Path::remove_tree($ROOT) };
  122. }
  123. }
  124. diag("sandbox: $ROOT");
  125. require Trog::Log;
  126. Trog::Log::log_init( 'logs/tcms.log', 'info' ); # sets $LOGDIR, hence logs/log.db
  127. require Trog::Config;
  128. require Trog::Themes;
  129. require Trog::Data;
  130. require Trog::Auth;
  131. require Trog::SQLite::TagIndex if $MODEL eq 'FlatFile';
  132. require Trog::Routes::HTML;
  133. require Trog::Routes::JSON;
  134. #--------------------------------------------------------------------------
  135. # A stand-in for the tPSGI object the router injects as $query->{tpsgi}.
  136. #--------------------------------------------------------------------------
  137. {
  138. package Test::TPSGI;
  139. sub new {
  140. my ( $class, %args ) = @_;
  141. return bless {
  142. callbacks => 0,
  143. run_callbacks => 0,
  144. renders => {},
  145. invalidated => [],
  146. watches => [],
  147. restarts => 0,
  148. tpsgi_dir => Cwd::getcwd(),
  149. log_dir => 'logs',
  150. gid => $),
  151. verbose => 0,
  152. %args,
  153. }, $class;
  154. }
  155. sub INFO { 1 }
  156. sub DEBUG { 1 }
  157. sub WARN { 1 }
  158. sub ERROR { 1 }
  159. sub CRIT { 1 }
  160. sub ALERT { 1 }
  161. sub EMERG { 1 }
  162. sub FATAL { 1 }
  163. sub see_also { return [ 303, [ 'Location' => $_[1], 'Content-Length' => 0 ], [''] ] }
  164. sub redirect { return [ 302, [ 'Location' => $_[1], 'Content-Length' => 0 ], [''] ] }
  165. sub redirect_permanent { return [ 301, [ 'Location' => $_[1], 'Content-Length' => 0 ], [''] ] }
  166. sub _reply { return [ $_[1], [ 'Content-type' => 'text/html' ], [ defined $_[2] ? $_[2] : '' ] ] }
  167. sub ok { return $_[0]->_reply( 200, $_[2] ) }
  168. sub notfound { return $_[0]->_reply( 404, $_[2] // 'Not Found' ) }
  169. sub forbidden { return $_[0]->_reply( 403, $_[2] // 'Forbidden' ) }
  170. sub badrequest { return $_[0]->_reply( 400, $_[2] // 'Bad Request' ) }
  171. sub error { return $_[0]->_reply( 500, $_[2] // 'Error' ) }
  172. sub serve { return [ 200, [], [''] ] }
  173. # These two must be inert.
  174. #
  175. # add_post_close_callback is the ONLY path by which Trog::Renderer and
  176. # post_save reach save_render()/invalidate_renders(). Dropping the callback
  177. # on the floor is what guarantees this test never writes into www/statics,
  178. # whatever the renderer's own skip-save logic decides.
  179. # ...unless a test explicitly asks, which is how the one test that is about
  180. # what gets cached gets to see it. Still nowhere near www/statics:
  181. # save_render below only records what it was handed.
  182. sub add_post_close_callback {
  183. my ( $self, $callback ) = @_;
  184. $self->{callbacks}++;
  185. $callback->() if $self->{run_callbacks} && ref $callback eq 'CODE';
  186. return 1;
  187. }
  188. # The real one is `kill 'HUP', getppid`. Under prove, that is prove.
  189. sub signal_restart_parent { $_[0]{restarts}++; return 1 }
  190. # Recorded rather than inert: a datasource that watches something is
  191. # supposed to register here, and the lifecycle test is where we find out
  192. # whether it did through the real route.
  193. sub add_watch {
  194. my ( $self, $path, $callback, %options ) = @_;
  195. push( @{ $self->{watches} }, { path => $path, callback => $callback, %options } );
  196. return 1;
  197. }
  198. sub invalidate_renders { push @{ $_[0]{invalidated} }, $_[1]; return 1 }
  199. sub invalidate_render { push @{ $_[0]{invalidated} }, [ @_[ 1 .. $#_ ] ]; return 1 }
  200. sub save_render { $_[0]{renders}{"$_[1]:$_[2]"} = $_[3]; return 1 }
  201. }
  202. our $TPSGI = Test::TPSGI->new();
  203. our $ADMIN_USER = 'specadmin';
  204. our $ADMIN_DISPLAY = 'Spec Admin';
  205. our $ADMIN_EMAIL = 'specadmin@example.com';
  206. our $ADMIN_PASS = 'spec-password';
  207. #--------------------------------------------------------------------------
  208. # Harness
  209. #--------------------------------------------------------------------------
  210. # Always build a fresh query: posts() pushes onto user_acls, and series()
  211. # rewrites route/aclname/tag/in_series/primary_post in place.
  212. sub _query {
  213. my (%over) = @_;
  214. my %q = (
  215. route => '/',
  216. method => 'GET',
  217. scheme => 'http', # Renderer::Base compares this with no undef guard
  218. domain => 'tcms.test',
  219. start => [ Time::HiRes::gettimeofday() ], # tv_interval() dies on anything else
  220. deflate => 0,
  221. streaming => 0,
  222. ranges => [],
  223. last_fetched => undef,
  224. has_query => 0,
  225. social_meta => 0,
  226. acls => [],
  227. user_acls => [],
  228. body => undef,
  229. tpsgi => $TPSGI,
  230. %over,
  231. );
  232. $q{fullpath} //= $q{route};
  233. return \%q;
  234. }
  235. sub _anon { return _query( user => undef, user_acls => [], @_ ) }
  236. sub _admin { return _query( user => $ADMIN_USER, user_acls => ['admin'], @_ ) }
  237. # Returns ($code, $body, $error). Never dies, so one bad post type cannot take
  238. # the rest of the file with it -- which matters because a failed render inside
  239. # index() dies rather than returning a 500 (there is no www/templates/html/500.tx).
  240. sub _render {
  241. my ( $query, $how ) = @_;
  242. $how ||= \&Trog::Routes::HTML::index;
  243. my $res = eval { $how->($query) };
  244. return ( 0, '', "died: $@" ) if !$res;
  245. return ( 0, '', 'returned a ' . ( ref($res) || 'plain scalar' ) ) if ref $res ne 'ARRAY';
  246. my ( $code, $headers, $body ) = @$res;
  247. $body = ref $body eq 'ARRAY' ? join( '', @$body ) : ( $body // '' );
  248. return ( $code, Encode::decode_utf8($body), '', $headers );
  249. }
  250. # FlatFile snapshots the tag list at load time and then filters each query's
  251. # tags against it IN PLACE -- so a tag created later isn't merely unindexed, it
  252. # is deleted from the query, and the filter silently degrades to "every public
  253. # post". Production gets away with it because a save HUPs the parent and the
  254. # worker re-execs. We have to do it by hand.
  255. #
  256. # Nothing to do for a model that asks the database each time, which is the
  257. # point: this helper exists to paper over a cache, and a model without one
  258. # needs no paper.
  259. sub _reindex {
  260. return unless $MODEL eq 'FlatFile';
  261. no warnings 'once';
  262. @Trog::Data::FlatFile::tags = Trog::SQLite::TagIndex::tags();
  263. %Trog::Data::FlatFile::posts_by_tag = ();
  264. return;
  265. }
  266. sub _data { return Trog::Data->new( Trog::Config::get() ) }
  267. # categories.tx renders into <span id="categories">. Scope to it -- index.tx
  268. # emits other class="topbar" anchors outside that span.
  269. sub _topbar_titles {
  270. my ($body) = @_;
  271. my ($span) = $body =~ m{<span id="categories">(.*?)</span>}s;
  272. return () unless defined $span;
  273. return $span =~ m{class="topbar">([^<]*)</a>}g;
  274. }
  275. # post_title.tx emits exactly one of these per displayed post.
  276. sub _post_count {
  277. my $n = () = $_[0] =~ m{<h3 class='blogtitles'>}g;
  278. return $n;
  279. }
  280. # The rendered page is ~40KB and mostly JavaScript. Show the content well.
  281. sub _kontent {
  282. my ($body) = @_;
  283. my ($k) = $body =~ m{<div id="kontent" class="kontained">(.*?)<div id="rightbar"}s;
  284. $k //= $body;
  285. $k =~ s/<script\b.*?<\/script>//gs;
  286. return length($k) > 2500 ? substr( $k, 0, 2500 ) . "\n...[truncated]" : $k;
  287. }
  288. # Save through post_save() rather than $data->add(): add() dies with an
  289. # arrayref of validator errors, whereas post_save catches them and hands back a
  290. # 400 with the field-level detail in the body -- which is the single most
  291. # useful thing to see when a fixture is wrong.
  292. sub _save_post {
  293. my ( $label, %post ) = @_;
  294. my $to = delete $post{to};
  295. my $res = eval { Trog::Routes::HTML::post_save( _admin( route => '/post/save', method => 'POST', to => $to, %post ) ) };
  296. if ( !$res || ref $res ne 'ARRAY' ) {
  297. fail("$label: saved");
  298. diag( $@ || 'post_save returned nothing' );
  299. return undef;
  300. }
  301. my ( $code, $headers, $body ) = @$res;
  302. my %h = @{ $headers || [] };
  303. # Both outcomes redirect now, so the status alone proves nothing -- the
  304. # outcome is in the query string post_save hands to the destination.
  305. if ( $code != 303 || ( $h{Location} // '' ) =~ m/savefailed=/ ) {
  306. fail("$label: saved");
  307. diag( "post_save returned $code, Location: " . ( $h{Location} // '(none)' ) );
  308. diag( join( '', @{ $body || [''] } ) ) if $body && @$body;
  309. return undef;
  310. }
  311. pass("$label: saved");
  312. _reindex(); # mandatory: see the comment on _reindex
  313. my $saved = _find_post( $post{title} );
  314. ok( $saved, "$label: readable back out of the datastore" ) or return undef;
  315. return $saved;
  316. }
  317. sub _find_post {
  318. my ($title) = @_;
  319. # filter() short-circuits on title; the admin acl skips the visibility filter.
  320. my @posts = _data()->get( title => $title, acls => ['admin'], limit => 0 );
  321. return $posts[0];
  322. }
  323. #--------------------------------------------------------------------------
  324. subtest 'the sandbox is what we are actually running against' => sub {
  325. is( Cwd::getcwd(), Cwd::abs_path($ROOT), 'cwd is the sandbox' );
  326. ok( -d 'data/files', 'datastore dir exists' );
  327. ok( -f 'config/default.cfg', 'config was seeded' );
  328. ok( !-f 'config/main.cfg', 'and there is no main.cfg, so we fall through to the default' );
  329. my $conf = Trog::Config::get();
  330. is( $conf->param('general.data_model'), $MODEL, 'config resolves inside the sandbox' );
  331. like( Trog::Themes::forms_dir(), qr/^www\/templates/, 'forms dir is relative, hence sandboxed' );
  332. ok( -d Cwd::abs_path( Trog::Themes::forms_dir() ), 'and it exists' );
  333. like( Cwd::abs_path( Trog::Themes::forms_dir() ), qr/^\Q$ROOT\E/, 'and it is under the sandbox, not the repo' );
  334. };
  335. subtest 'registering the first admin bootstraps the install' => sub {
  336. ok( !-f 'config/has_users', 'no users yet, so login() is in register mode' );
  337. my ( $code, $body, $err, $headers ) = _render(
  338. _anon(
  339. route => '/login',
  340. method => 'POST',
  341. username => $ADMIN_USER,
  342. display_name => $ADMIN_DISPLAY,
  343. contact_email => $ADMIN_EMAIL,
  344. password => $ADMIN_PASS,
  345. to => '/',
  346. ),
  347. \&Trog::Routes::HTML::login,
  348. );
  349. is( $code, 200, 'login() rendered' ) or diag($err);
  350. my %h = @{ $headers || [] };
  351. like( $h{'Set-Cookie'} // '', qr/^tcmslogin=[0-9a-f-]{36};/, 'a session cookie came back' );
  352. ok( -f 'config/has_users', 'registration is now closed' );
  353. is_deeply( Trog::Auth::acls4user($ADMIN_USER), ['admin'], 'the new user is an admin' );
  354. _reindex();
  355. my @seeded = _data()->get( acls => ['admin'], limit => 0 );
  356. is( scalar @seeded, 4, 'the four seed posts were written' )
  357. or diag( 'got: ' . join( ', ', map { "$_->{title} ($_->{form})" } @seeded ) );
  358. };
  359. subtest 'the post wizard invents a new post type' => sub {
  360. my %wizard = (
  361. name => 'spec_widget',
  362. title_placeholder => 'Widget Name',
  363. body_form => 'form_common.tx',
  364. wrapper => 1,
  365. inc_post_title => 1,
  366. inc_post_tags => 1,
  367. inc_title_input => 1,
  368. inc_visibility => 1,
  369. inc_tags => 1,
  370. inc_aliases => 1,
  371. param_name => ['flavor'],
  372. param_type => ['text'],
  373. param_label => ['Flavor'],
  374. param_placeholder => ['Strawberry'],
  375. param_required => [0],
  376. display => q{<div class="postData responsive-text" id="postData-<: $post.id :>">} . q{<span class="spec-flavor"><: $post.flavor :></span>} . q{<: render_it($post.data) | mark_raw :></div>},
  377. );
  378. my ( $code, $body, $err ) = _render(
  379. _admin( route => '/admin/wyzzerdd/save', method => 'POST', %wizard ),
  380. \&Trog::Routes::HTML::post_wizard_save,
  381. );
  382. is( $code, 200, 'wizard save rendered' ) or diag($err);
  383. ok( -f 'www/templates/html/components/forms/spec_widget.tx', 'the template was written' );
  384. ok( -f 'www/templates/html/components/forms/spec_widget.json', 'the schema sidecar was written' );
  385. like( $body, qr{<li>spec_widget\.tx</li>}, 'and the new type is listed back to us' );
  386. # This is the assertion that the sandbox is real: a symlinked template tree
  387. # would have put these in the developer's actual checkout.
  388. ok( !-e "$REPO/www/templates/html/components/forms/spec_widget.tx", 'nothing was written into the repo' );
  389. # The relation controls have to be present in every row and always submit,
  390. # or the parallel param_* arrays de-align server side.
  391. like( $body, qr/name="param_relation_form"/, 'the relation target select is in the row template' );
  392. like( $body, qr/name="param_relation_mode"/, 'so is the relation mode select' );
  393. like( $body, qr/<option value="relation">/, 'and relation is offered as a field type' );
  394. # A series can ask for its children to be tiled. A generated type has to
  395. # carry that on its wrapper like the hand-written forms do, or it is the
  396. # one kind of post that silently ignores the setting.
  397. my $generated = Path::Tiny->new('www/templates/html/components/forms/spec_widget.tx')->slurp_utf8;
  398. # Single quoted: the wrapper is Kolon source, so every sigil in it is
  399. # literal and must not be interpolated by the test that checks for it.
  400. my $wrapper = q{<div class="post <: $style :> <: $tiled ? 'tile' : '' :>">};
  401. ok( index( $generated, $wrapper ) >= 0, 'the generated wrapper carries the tile class when the series asks for it' )
  402. or diag( "wanted: $wrapper\ngot: " . ( split( /\n/, $generated ) )[1] );
  403. # Every post has a title, a visibility and acls. The wizard used to let a
  404. # type opt out of collecting them, which is how a hypervisor post ended up
  405. # stored with no visibility and an undef in its tags.
  406. foreach my $required ( 'visibility.tx', 'acls.tx' ) {
  407. like( $generated, qr/\Q: include "$required";\E/, "$required is included whether or not it was asked for" );
  408. }
  409. like( $generated, qr/name="title"/, 'and so is the title input' );
  410. foreach my $gone (qw{inc_title_input inc_visibility inc_acls}) {
  411. unlike( $body, qr/name="\Q$gone\E"/, "the wizard no longer offers '$gone' as a choice" );
  412. }
  413. like( $body, qr/name="param_private"/, 'and it offers per-field privacy' );
  414. like( $body, qr/name="datasource"/, 'and a datasource to draw the posts from' );
  415. like( $body, qr/Trog::DataSource::Virt/, 'listing the ones that exist' );
  416. };
  417. subtest 'a datasource-backed type gets no editor' => sub {
  418. # A datasource whose posts are built rather than written has nothing behind
  419. # a form to save, so generating one would produce an editor that writes
  420. # real posts to sit alongside the synthesized ones.
  421. my ( $code, $body, $err ) = _render(
  422. _admin(
  423. route => '/admin/wyzzerdd/save',
  424. method => 'POST',
  425. name => 'spec_sourced',
  426. datasource => 'Trog::DataSource::Virt',
  427. body_form => 'form_common.tx',
  428. wrapper => 1,
  429. inc_post_title => 1,
  430. display => q{<div class="s"><: $post.title :></div>},
  431. ),
  432. \&Trog::Routes::HTML::post_wizard_save,
  433. );
  434. is( $code, 200, 'the type was created' ) or diag($err);
  435. my $generated = Path::Tiny->new('www/templates/html/components/forms/spec_sourced.tx')->slurp_utf8;
  436. unlike( $generated, qr{action="/post/save"}, 'no editor was generated' );
  437. unlike( $generated, qr/\$can_edit/, 'and nothing guards one' );
  438. like( $generated, qr/class="s"/, 'but the display half is there' );
  439. my $sidecar = JSON::MaybeXS::decode_json( Path::Tiny->new('www/templates/html/components/forms/spec_sourced.json')->slurp_utf8 );
  440. is( $sidecar->{'x-tcms-datasource'}, 'Trog::DataSource::Virt', 'and the sidecar records the datasource' );
  441. # The same type without one keeps its editor.
  442. _render(
  443. _admin(
  444. route => '/admin/wyzzerdd/save',
  445. method => 'POST',
  446. name => 'spec_stored',
  447. body_form => 'form_common.tx',
  448. wrapper => 1,
  449. inc_post_title => 1,
  450. display => q{<div class="s"><: $post.title :></div>},
  451. ),
  452. \&Trog::Routes::HTML::post_wizard_save,
  453. );
  454. my $stored = Path::Tiny->new('www/templates/html/components/forms/spec_stored.tx')->slurp_utf8;
  455. like( $stored, qr{action="/post/save"}, 'a type on the datastore still gets one' );
  456. # The name is require()d later, so it has to be one we found.
  457. ( $code, $body ) = _render(
  458. _admin( route => '/admin/wyzzerdd/save', method => 'POST', name => 'spec_bogus', datasource => 'Evil::Module' ),
  459. \&Trog::Routes::HTML::post_wizard_save,
  460. );
  461. like( $body, qr/not a datasource I can find/, 'a datasource we did not find is refused' );
  462. ok( !-e 'www/templates/html/components/forms/spec_bogus.tx', 'and nothing was written' );
  463. };
  464. subtest 'a datasource-backed series searches and paginates' => sub {
  465. # The whole point of a datasource is that its posts are built rather than
  466. # stored, so nothing the datastore does to a query ever touches them. Stand
  467. # one up with enough guests to page through, and drive it through the real
  468. # route rather than calling the helpers.
  469. require Test::MockModule;
  470. require Trog::DataSource::Virt;
  471. my ( $code, $body, $err ) = _render(
  472. _admin(
  473. route => '/admin/wyzzerdd/save',
  474. method => 'POST',
  475. name => 'spec_guests',
  476. datasource => 'Trog::DataSource::Virt',
  477. body_form => 'form_common.tx',
  478. wrapper => 1,
  479. inc_post_title => 1,
  480. display => q{<div class="guest"><: $post.title :> is <: $post.state :></div>},
  481. ),
  482. \&Trog::Routes::HTML::post_wizard_save,
  483. );
  484. is( $code, 200, 'the datasource-backed type was created' ) or diag($err);
  485. _reindex();
  486. my $series = _make_series( 'spec_guests', 'specguests', 'spec_guests.tx', 'Spec Guests Series', no_topbar => 1 ) or return;
  487. # Twelve guests, every one stamped with the same created -- which is what
  488. # libvirt guests really are, and precisely what the cursor paginator cannot
  489. # page through.
  490. my $built = time();
  491. my $virt = Test::MockModule->new('Trog::DataSource::Virt');
  492. $virt->redefine(
  493. posts => sub {
  494. my ( $s, $q ) = @_;
  495. return map {
  496. {
  497. id => "guest-$_",
  498. title => sprintf( 'guest%02d', $_ ),
  499. form => $s->{child_form},
  500. data => '',
  501. state => $_ % 2 ? 'running' : 'shut off',
  502. local_href => "/guest/hv/guest$_",
  503. href => '',
  504. tags => $s->{tags} // [],
  505. visibility => $s->{visibility} // 'private',
  506. created => $built,
  507. version => 0,
  508. user => $s->{user},
  509. method => 'GET',
  510. }
  511. } 1 .. 12;
  512. }
  513. );
  514. my $page_of = sub {
  515. my (%q) = @_;
  516. my ( $c, $b, $e ) = _render( _anon( route => '/specguests', %q ), \&Trog::Routes::HTML::series );
  517. is( $c, 200, 'guests page renders' ) or diag($e);
  518. return $b // '';
  519. };
  520. my $titles = sub {
  521. my ($html) = @_;
  522. return [ $html =~ m{<div class="guest">(guest\d+) is}g ];
  523. };
  524. my $first = $page_of->( limit => 5 );
  525. is_deeply( $titles->($first), [qw{guest01 guest02 guest03 guest04 guest05}], 'the first page holds the first five, in a settled order' );
  526. like( $first, qr/Page 1 of 3/, 'and says which page it is' );
  527. like( $first, qr{href="\?page=2&limit=5}, 'offering the next one' );
  528. unlike( $first, qr/rel="prev"/, 'and no previous one' );
  529. my $second = $page_of->( limit => 5, page => 2 );
  530. is_deeply( $titles->($second), [qw{guest06 guest07 guest08 guest09 guest10}], 'the second page carries on where the first stopped' );
  531. like( $second, qr/Page 2 of 3/, 'and says so' );
  532. my $third = $page_of->( limit => 5, page => 3 );
  533. is_deeply( $titles->($third), [qw{guest11 guest12}], 'the last page holds the remainder' );
  534. unlike( $third, qr/rel="next"/, 'and offers nothing after it' );
  535. # A guest's state is the point of the page, and nothing could invalidate a
  536. # cached copy: a guest starting is not a post being saved, and libvirt is
  537. # not going to tell us about it.
  538. my $vtpsgi = Test::TPSGI->new( run_callbacks => 1 );
  539. _render( _anon( route => '/specguests', tpsgi => $vtpsgi ), \&Trog::Routes::HTML::series );
  540. is_deeply( [ keys %{ $vtpsgi->{renders} } ], [], 'a guests page is never saved as a static' );
  541. # The cursor paginator would be worse than useless here: every guest shares
  542. # a created, so Prev would ask for everything older than that one instant.
  543. unlike( $first, qr/older=$built/, 'no cursor is offered on a page that cannot use one' );
  544. # Search, which before this reached the datastore and was thrown away along
  545. # with the posts it filtered.
  546. my $searched = $page_of->( limit => 25, like => 'guest1' );
  547. is_deeply( $titles->($searched), [qw{guest10 guest11 guest12}], 'searching a datasource page searches the page' );
  548. # Virt searches what its posts carry, not just a title and an empty body.
  549. my $by_state = $page_of->( limit => 25, like => 'shut' );
  550. is_deeply( $titles->($by_state), [qw{guest02 guest04 guest06 guest08 guest10 guest12}], 'including a field only it knows about' );
  551. # And the two compose, which is what a reader paging through a search does.
  552. my $searched_page = $page_of->( limit => 4, like => 'shut', page => 2 );
  553. is_deeply( $titles->($searched_page), [qw{guest10 guest12}], 'paging through a search stays inside it' );
  554. like( $searched_page, qr/Page 2 of 2/, 'and pages against the matches, not everything' );
  555. like( $searched_page, qr/like=shut/, 'carrying the search into the links' );
  556. };
  557. subtest 'an ordinary series survives a page number in its URL' => sub {
  558. # series() looks its own post up with the reader's query, so a ?page= meant
  559. # for the children used to page the lookup of the series itself -- page two
  560. # of one post being nothing, the series could not find itself and 404'd on
  561. # its own page. Latent until the datasource paginator started emitting page
  562. # numbers, and it was never about datasources.
  563. _reindex();
  564. _make_series( 'spec_paged', 'specpaged', 'blog.tx', 'Spec Paged Series', no_topbar => 1 ) or return;
  565. foreach my $n ( 1, 2 ) {
  566. _save_post(
  567. "spec_paged child $n",
  568. form => 'blog.tx',
  569. title => "Spec Paged Child $n",
  570. data => "child $n body",
  571. visibility => 'public',
  572. tags => ['specpaged'],
  573. to => '/specpaged',
  574. ) or return;
  575. }
  576. # One child a page, so page two exists and holds the other one.
  577. my ( $code, $body, $err ) = _render( _anon( route => '/specpaged', limit => 1, page => 2 ), \&Trog::Routes::HTML::series );
  578. is( $code, 200, 'a series page with ?page=2 still finds its series' ) or diag($err);
  579. like( $body, qr/Spec Paged Series/, 'and is the right one' );
  580. ( $code, $body, $err ) = _render( _anon( route => '/specpaged', limit => 1, page => 1 ), \&Trog::Routes::HTML::series );
  581. is( $code, 200, 'as does page one' ) or diag($err);
  582. # A stored series still pages by cursor, not by number: nothing about this
  583. # fix moves it onto the datasource paginator.
  584. unlike( $body, qr/Page 1 of/, 'and it still pages by cursor rather than by number' );
  585. };
  586. subtest 'a directory index series' => sub {
  587. # A wizard-built type drawing its posts from a directory, through the real
  588. # route, on whichever data model this run is using.
  589. require Trog::DataSource::DirIndex;
  590. my ( $code, $body, $err ) = _render(
  591. _admin(
  592. route => '/admin/wyzzerdd/save',
  593. method => 'POST',
  594. name => 'spec_files',
  595. datasource => 'Trog::DataSource::DirIndex',
  596. body_form => 'form_common.tx',
  597. wrapper => 1,
  598. inc_post_title => 1,
  599. display => q{<div class="entry"><: $post.name :> (<: $post.size_human :>)</div>},
  600. ),
  601. \&Trog::Routes::HTML::post_wizard_save,
  602. );
  603. is( $code, 200, 'the directory-backed type was created' ) or diag($err);
  604. File::Path::make_path('www/assets/spec_downloads');
  605. foreach my $n ( 1 .. 7 ) {
  606. open( my $fh, '>', "www/assets/spec_downloads/file$n.txt" ) or die $!;
  607. print {$fh} ( 'x' x ( $n * 10 ) );
  608. close $fh;
  609. }
  610. _reindex();
  611. my $series = _make_series(
  612. 'spec_files', 'specfiles', 'spec_files.tx', 'Spec Files Series',
  613. no_topbar => 1,
  614. directory => 'assets/spec_downloads',
  615. ) or return;
  616. is( $series->{directory}, 'assets/spec_downloads', 'the series remembers which directory it indexes' );
  617. my $names = sub {
  618. my ($html) = @_;
  619. return [ $html =~ m{<div class="entry">(file\d+\.txt) \(}g ];
  620. };
  621. ( $code, $body, $err ) = _render( _anon( route => '/specfiles' ), \&Trog::Routes::HTML::series );
  622. is( $code, 200, 'the listing renders' ) or diag($err);
  623. is_deeply( $names->($body), [ map { "file$_.txt" } 1 .. 7 ], 'listing the directory, in name order' );
  624. like( $body, qr/\(10 B\)/, 'with each entry\'s size' );
  625. # post_title.tx quotes with single quotes; the point is the target, not the quoting.
  626. like( $body, qr{href=['"]/assets/spec_downloads/file1\.txt['"]}, 'and a link to the file itself' );
  627. # It paginates like any other datasource page.
  628. ( $code, $body, $err ) = _render( _anon( route => '/specfiles', limit => 3, page => 2 ), \&Trog::Routes::HTML::series );
  629. is( $code, 200, 'page two renders' ) or diag($err);
  630. is_deeply( $names->($body), [qw{file4.txt file5.txt file6.txt}], 'holding the next three' );
  631. like( $body, qr/Page 2 of 3/, 'and saying so' );
  632. # And searches like one.
  633. ( $code, $body, $err ) = _render( _anon( route => '/specfiles', limit => 25, like => 'file3' ), \&Trog::Routes::HTML::series );
  634. is_deeply( $names->($body), ['file3.txt'], 'searching the listing searches the filenames' );
  635. # The point of the exercise: this page is cached like any other, and
  636. # nothing else would ever throw that cache away, because no post is saved
  637. # when somebody drops a file in a directory.
  638. my $tpsgi = Test::TPSGI->new( run_callbacks => 1 );
  639. _render( _anon( route => '/specfiles', tpsgi => $tpsgi ), \&Trog::Routes::HTML::series );
  640. # And it really is cached, rather than merely being cacheable in principle:
  641. # the renderer hands anonymous, unparameterised, successful renders to tPSGI
  642. # to save, and a directory listing is one. Which it may be, unlike the
  643. # guests page above, precisely because it registers the watch below.
  644. ok( scalar( keys %{ $tpsgi->{renders} } ), 'the listing was handed to tPSGI to save as a static' );
  645. ok( ( grep { m{^/specfiles:} } keys %{ $tpsgi->{renders} } ), 'under its own route' );
  646. my ($watch) = grep { $_->{path} =~ m/spec_downloads/ } @{ $tpsgi->{watches} };
  647. ok( $watch, 'rendering it registers a watch on the directory' );
  648. like( $watch->{key}, qr/DirIndex/, 'under a stable key, so a view does not stack another' );
  649. $watch->{callback}->( $tpsgi, { path => 'www/assets/spec_downloads/file8.txt', events => ['CREATE'] } );
  650. is_deeply( $tpsgi->{invalidated}, ['html'], 'and a change to the directory invalidates the renders' );
  651. };
  652. subtest 'a field can be declared indexed' => sub {
  653. my ( $code, $body, $err ) = _render(
  654. _admin(
  655. route => '/admin/wyzzerdd/save',
  656. method => 'POST',
  657. name => 'spec_indexed',
  658. body_form => 'form_common.tx',
  659. wrapper => 1,
  660. inc_post_title => 1,
  661. # The middle row is the one that matters: its box is clear, and the
  662. # wizard zips these arrays together positionally, so if a clear box
  663. # ever submitted nothing the flag would land on the wrong field.
  664. param_name => [ 'cook_time', 'garnish', 'serves' ],
  665. param_type => [ 'text', 'text', 'number' ],
  666. param_label => [ 'Cook Time', 'Garnish', 'Serves' ],
  667. param_placeholder => [ '', '', '' ],
  668. param_required => [ 0, 0, 0 ],
  669. param_private => [ 0, 0, 0 ],
  670. param_indexed => [ 1, 0, 1 ],
  671. param_relation_form => [ 'blog.tx', 'blog.tx', 'blog.tx' ],
  672. param_relation_mode => [ 'one', 'one', 'one' ],
  673. display => q{<div class="ct"><: $post.cook_time :></div>},
  674. ),
  675. \&Trog::Routes::HTML::post_wizard_save,
  676. );
  677. is( $code, 200, 'the type was created' ) or diag($err);
  678. my $sidecar = JSON::MaybeXS::decode_json( Path::Tiny->new('www/templates/html/components/forms/spec_indexed.json')->slurp_utf8 );
  679. ok( $sidecar->{properties}{cook_time}{'x-tcms-indexed'}, 'the ticked field is marked indexed' );
  680. ok( $sidecar->{properties}{serves}{'x-tcms-indexed'}, 'and so is the one after the clear box' );
  681. ok( !exists $sidecar->{properties}{garnish}{'x-tcms-indexed'}, 'the field between them is not' );
  682. ok( !exists $sidecar->{properties}{cook_time}{'x-tcms-private'}, 'and indexing is not privacy' );
  683. is_deeply(
  684. Trog::DataModule::indexed_fields_for('spec_indexed.tx'),
  685. [ sort qw{cook_time serves} ],
  686. 'and the model reads them back out of the sidecar'
  687. );
  688. SKIP: {
  689. skip( "the $MODEL model has nothing to index with", 3 ) unless $MODEL eq 'SQLite';
  690. my $dbh = Trog::SQLite::dbh( undef, 'data/posts.sqlite' );
  691. my %columns = map { $_->{name} => 1 } @{ $dbh->selectall_arrayref( 'PRAGMA table_xinfo(posts)', { Slice => {} } ) };
  692. my %indexes = map { $_->{name} => 1 } @{ $dbh->selectall_arrayref( "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='posts'", { Slice => {} } ) };
  693. ok( $columns{cook_time}, 'saving the type gave the ticked field a column' );
  694. ok( $indexes{'posts_custom_cook_time'}, 'and an index' );
  695. ok( !$indexes{'posts_custom_garnish'}, 'and left the unticked one alone' );
  696. }
  697. # A post of the type still saves and reads back the same as any other.
  698. _reindex();
  699. _make_series( 'spec_indexed', 'specindexed', 'spec_indexed.tx', 'Spec Indexed Series', no_topbar => 1 ) or return;
  700. my $child = _save_post(
  701. 'spec_indexed child',
  702. form => 'spec_indexed.tx',
  703. title => 'Spec Indexed Child',
  704. cook_time => '45 minutes',
  705. garnish => 'parsley',
  706. visibility => 'public',
  707. tags => ['specindexed'],
  708. to => '/specindexed',
  709. ) or return;
  710. is( $child->{cook_time}, '45 minutes', 'an indexed field stores like any other' );
  711. is( $child->{garnish}, 'parsley', 'and so does the one beside it' );
  712. };
  713. subtest 'a field can be declared editors-only' => sub {
  714. my ( $code, $body, $err ) = _render(
  715. _admin(
  716. route => '/admin/wyzzerdd/save',
  717. method => 'POST',
  718. name => 'spec_secretive',
  719. body_form => 'form_common.tx',
  720. wrapper => 1,
  721. inc_post_title => 1,
  722. inc_tags => 1,
  723. param_name => [ 'shown', 'hidden' ],
  724. param_type => [ 'text', 'text' ],
  725. param_label => [ 'Shown', 'Hidden' ],
  726. param_placeholder => [ '', '' ],
  727. param_required => [ 0, 0 ],
  728. param_private => [ 0, 1 ],
  729. param_relation_form => [ 'blog.tx', 'blog.tx' ],
  730. param_relation_mode => [ 'one', 'one' ],
  731. # Deliberately unguarded: declaring the field private is supposed
  732. # to be enough, so an author does not have to remember.
  733. display => q{<div class="s"><: $post.shown :></div><div class="h"><: $post.hidden :></div>},
  734. ),
  735. \&Trog::Routes::HTML::post_wizard_save,
  736. );
  737. is( $code, 200, 'the type was created' ) or diag($err);
  738. my $sidecar = JSON::MaybeXS::decode_json( Path::Tiny->new('www/templates/html/components/forms/spec_secretive.json')->slurp_utf8 );
  739. ok( $sidecar->{properties}{hidden}{'x-tcms-private'}, 'the private field is marked so' );
  740. ok( !exists $sidecar->{properties}{shown}{'x-tcms-private'}, 'and a public one carries no flag at all' );
  741. _reindex();
  742. _make_series( 'spec_secretive', 'specsecretive', 'spec_secretive.tx', 'Spec Secretive Series', no_topbar => 1 ) or return;
  743. my $child = _save_post(
  744. 'spec_secretive child',
  745. form => 'spec_secretive.tx',
  746. title => 'Spec Secretive Child',
  747. shown => 'public value',
  748. hidden => 'private value',
  749. visibility => 'public',
  750. tags => ['specsecretive'],
  751. to => '/specsecretive',
  752. ) or return;
  753. is( $child->{hidden}, 'private value', 'the value is stored like any other' );
  754. # An editor sees both.
  755. my ( $acode, $abody ) = _render( _admin( route => '/specsecretive' ), \&Trog::Routes::HTML::series );
  756. is( $acode, 200, 'the series renders for an admin' );
  757. like( $abody, qr{<div class="h">private value</div>}, 'who sees the private field' );
  758. # A logged out reader sees only the public one, without the template
  759. # having guarded anything.
  760. my ( $ncode, $nbody ) = _render( _anon( route => '/specsecretive' ), \&Trog::Routes::HTML::series );
  761. is( $ncode, 200, 'and for everyone else' );
  762. like( $nbody, qr{<div class="s">public value</div>}, 'who still sees the public field' );
  763. unlike( $nbody, qr/private value/, 'but not the private one' );
  764. };
  765. #--------------------------------------------------------------------------
  766. # The matrix.
  767. #
  768. # Per type: build a topbar series whose child_form is that type, check it shows
  769. # up in the nav, add a child post, check the child renders on the series page.
  770. #
  771. # Everything is verified ANONYMOUSLY. can_edit is then false, so the editor
  772. # half of each form never renders and a title appears exactly once -- which is
  773. # what makes like($body, qr/$title/) mean anything. It also sidesteps the
  774. # /secure prefixing that both _post_helper and categories.tx apply when a user
  775. # is set.
  776. #--------------------------------------------------------------------------
  777. sub _make_series {
  778. my ( $label, $aclname, $child_form, $title, %opt ) = @_;
  779. # _get_series caps the bar at 10 and the seeds already use two, so only the
  780. # series whose topbar behaviour we actually care about get tagged.
  781. my @tags = $opt{no_topbar} ? ('series') : ( 'series', 'topbar' );
  782. my %extra = map { $_ => $opt{$_} } grep { $_ ne 'no_topbar' } keys(%opt);
  783. my $series = _save_post(
  784. "$label series",
  785. %extra,
  786. form => 'series.tx',
  787. title => $title,
  788. aclname => $aclname, # required by series.json
  789. child_form => $child_form, # required by series.json
  790. local_href => "/$aclname",
  791. href => "/$aclname",
  792. data => "$title subhead",
  793. callback => 'Trog::Routes::HTML::series',
  794. visibility => 'public',
  795. tags => \@tags,
  796. tiled => 0,
  797. to => '/',
  798. ) or return undef;
  799. return $series if $opt{no_topbar};
  800. # The topbar comes from _get_series(), which index() renders through
  801. # categories.tx. Check it on / -- the series has no children yet, and
  802. # posts() 404s on an empty series.
  803. my ( $code, $body, $err ) = _render( _anon( route => '/' ) );
  804. is( $code, 200, "$label: / renders" ) or diag($err);
  805. my @titles = _topbar_titles($body);
  806. ok( ( grep { $_ eq $title } @titles ), "$label: series is in the topbar" )
  807. or diag( 'topbar was: ' . join( ', ', @titles ) );
  808. return $series;
  809. }
  810. sub _series_page {
  811. my ( $label, $aclname, $series_title, $expected_posts ) = @_;
  812. my ( $code, $body, $err ) = _render( _anon( route => "/$aclname" ), \&Trog::Routes::HTML::series );
  813. is( $code, 200, "$label: series page renders" ) or do { diag($err); return '' };
  814. like( $body, qr{<title>[^<]*\Q$series_title\E}, "$label: series page is the right series" );
  815. is( _post_count($body), $expected_posts, "$label: exactly $expected_posts post(s) listed" )
  816. or diag( _kontent($body) );
  817. return $body;
  818. }
  819. # Entities first: the invoice type resolves payee/payor against existing public
  820. # entities.tx posts, so those have to be in the datastore before we get there.
  821. our ( $PAYEE, $PAYOR );
  822. subtest 'entities' => sub {
  823. my $title = 'Spec Entities Series';
  824. _make_series( 'entities', 'specentities', 'entities.tx', $title ) or return;
  825. $PAYEE = _save_post(
  826. 'entities payee child',
  827. form => 'entities.tx',
  828. title => 'Spec Payee Entity',
  829. data => 'Acme Payments LLC, 1 Road, Metropolis',
  830. payment_method => 'ACH',
  831. payment_details => 'Routing 0000 Account 1111',
  832. visibility => 'public',
  833. tags => ['specentities'],
  834. to => '/specentities',
  835. ) or return;
  836. $PAYOR = _save_post(
  837. 'entities payor child',
  838. form => 'entities.tx',
  839. title => 'Spec Payor Entity',
  840. data => 'Wile E Coyote, PO Box 9',
  841. payment_method => 'Carrier Pigeon',
  842. payment_details => 'none',
  843. visibility => 'public',
  844. tags => ['specentities'],
  845. to => '/specentities',
  846. ) or return;
  847. my $body = _series_page( 'entities', 'specentities', $title, 2 ) or return;
  848. like( $body, qr{id="postData-\Q$PAYEE->{id}\E"}, 'payee body rendered' );
  849. like( $body, qr/Acme Payments LLC/, 'payee data rendered' );
  850. like( $body, qr/Wile E Coyote/, 'payor data rendered' );
  851. # entity_details.tx is included only in the editor half, so these are stored
  852. # but never displayed. Pinning that so a template change is noticed.
  853. unlike( $body, qr/Carrier Pigeon/, 'payment details are not shown to anonymous readers' );
  854. is( _find_post('Spec Payee Entity')->{payment_method}, 'ACH', 'but payment_method did survive the schema' );
  855. };
  856. subtest 'blog' => sub {
  857. my $title = 'Spec Blog Series';
  858. _make_series( 'blog', 'specblog', 'blog.tx', $title ) or return;
  859. my $child = _save_post(
  860. 'blog child',
  861. form => 'blog.tx',
  862. title => 'Spec Blog Child',
  863. data => 'Spec blog body text.',
  864. visibility => 'public',
  865. tags => ['specblog'],
  866. to => '/specblog',
  867. ) or return;
  868. my $body = _series_page( 'blog', 'specblog', $title, 1 ) or return;
  869. like( $body, qr/Spec Blog Child/, 'child title rendered' );
  870. like( $body, qr{href='/posts/\Q$child->{id}\E'}, 'child permalink rendered' );
  871. like( $body, qr{id="postData-\Q$child->{id}\E"}, 'child body block rendered' );
  872. like( $body, qr/Spec blog body text\./, 'child body text rendered' );
  873. unlike( $body, qr/Spec Payee Entity/, 'no bleed from the entities series' );
  874. };
  875. subtest 'microblog' => sub {
  876. my $title = 'Spec Microblog Series';
  877. _make_series( 'microblog', 'specmicroblog', 'microblog.tx', $title ) or return;
  878. my $child = _save_post(
  879. 'microblog child',
  880. form => 'microblog.tx',
  881. title => 'Spec Microblog Child',
  882. href => 'https://example.com/spec',
  883. data => 'Spec microblog body.',
  884. visibility => 'public',
  885. tags => ['specmicroblog'],
  886. to => '/specmicroblog',
  887. ) or return;
  888. my $body = _series_page( 'microblog', 'specmicroblog', $title, 1 ) or return;
  889. like( $body, qr{href='https://example\.com/spec' >Spec Microblog Child</a>}, 'title links out to href' );
  890. like( $body, qr{id="postData-\Q$child->{id}\E"}, 'body block rendered' );
  891. like( $body, qr/Spec microblog body\./, 'body text rendered' );
  892. };
  893. subtest 'file' => sub {
  894. my $title = 'Spec File Series';
  895. _make_series( 'file', 'specfile', 'file.tx', $title ) or return;
  896. # _handle_upload MOVES the file, so it has to be created fresh here.
  897. my $src = "$ROOT/upload-src/spec-upload.txt";
  898. open( my $fh, '>', $src ) or die $!;
  899. print {$fh} "spec upload contents\n";
  900. close $fh;
  901. my $child = _save_post(
  902. 'file child',
  903. form => 'file.tx',
  904. title => 'Spec File Child',
  905. data => 'Spec file body.',
  906. file => { tempname => $src, filename => 'spec-upload.txt' },
  907. visibility => 'public',
  908. tags => ['specfile'],
  909. to => '/specfile',
  910. ) or return;
  911. ok( !-e $src, 'the upload was moved out of its temp location, not copied' );
  912. ok( -f "www/assets/$child->{id}.spec-upload.txt", 'and it landed in www/assets' );
  913. my $body = _series_page( 'file', 'specfile', $title, 1 ) or return;
  914. like( $body, qr{href='/assets/\Q$child->{id}\E\.spec-upload\.txt'}, 'title links to the asset' );
  915. like( $body, qr{id="postData-\Q$child->{id}\E"}, 'body block carries a unique id, like every other form' );
  916. like( $body, qr/Spec file body\./, 'body text rendered' );
  917. # This form used to emit <div class="postData" id="postData" class="responsive-text">
  918. # -- a duplicated class attribute, and an id shared by every file post on
  919. # the page. Neither should come back.
  920. unlike( $body, qr{<div class="postData" id="postData"}, 'no duplicated class attribute' );
  921. # text/plain, so none of the media branches fire.
  922. unlike( $body, qr/<video/, 'no video block for a text file' );
  923. unlike( $body, qr/<audio/, 'no audio block either' );
  924. };
  925. subtest 'presentation' => sub {
  926. my $title = 'Spec Presentation Series';
  927. _make_series( 'presentation', 'specpresentation', 'presentation.tx', $title ) or return;
  928. my $child = _save_post(
  929. 'presentation child',
  930. form => 'presentation.tx',
  931. title => 'Spec Presentation Child',
  932. data => [ 'Slide one body.', 'Slide two body.' ],
  933. data_is_array => 1,
  934. visibility => 'public',
  935. tags => ['specpresentation'],
  936. to => '/specpresentation',
  937. ) or return;
  938. is_deeply( $child->{data}, [ 'Slide one body.', 'Slide two body.' ], 'the multi-page data round-tripped as an array' );
  939. my $body = _series_page( 'presentation', 'specpresentation', $title, 1 ) or return;
  940. # Slides only render under nochrome; the series page just links to the deck.
  941. like( $body, qr{<a href="/posts/\Q$child->{id}\E\?embed=1&nochrome=1">Click Here to view</a>}, 'deck link rendered' );
  942. unlike( $body, qr/Slide one body\./, 'slides themselves are not rendered inline' );
  943. };
  944. subtest 'invoice' => sub {
  945. my $title = 'Spec Invoice Series';
  946. _make_series( 'invoice', 'specinvoice', 'invoice.tx', $title ) or return;
  947. my $child = _save_post(
  948. 'invoice child',
  949. form => 'invoice.tx',
  950. title => 'Spec Invoice Child',
  951. data => [ "Widget assembly\nPrice: \$420", "Shipping\nPrice: \$80" ],
  952. data_is_array => 1,
  953. payee => $PAYEE->{id},
  954. payor => $PAYOR->{id},
  955. due_days => 30,
  956. visibility => 'public',
  957. tags => ['specinvoice'],
  958. to => '/specinvoice',
  959. ) or return;
  960. my $body = _series_page( 'invoice', 'specinvoice', $title, 1 ) or return;
  961. like( $body, qr/Invoice ID# \Q$child->{id}\E/, 'invoice header rendered' );
  962. # These two only resolve because the entities subtest ran first and left
  963. # public entities.tx posts behind for the enrich hook to find.
  964. like( $body, qr/Acme Payments LLC/, 'payee entity resolved' );
  965. like( $body, qr/Wile E Coyote/, 'payor entity resolved' );
  966. like( $body, qr/ACH/, 'payee payment method pulled through' );
  967. like( $body, qr/Widget assembly/, 'line items rendered' );
  968. like( $body, qr/Total: \$500/, 'total summed from the line item prices' );
  969. };
  970. subtest 'spec_widget (the wizard-built type)' => sub {
  971. my $title = 'Spec Widget Series';
  972. _make_series( 'spec_widget', 'specwidget', 'spec_widget.tx', $title ) or return;
  973. my $child = _save_post(
  974. 'spec_widget child',
  975. form => 'spec_widget.tx',
  976. title => 'Spec Widget Child',
  977. data => 'Spec widget body.',
  978. flavor => 'Strawberry',
  979. bogus_field => 'nope',
  980. visibility => 'public',
  981. tags => ['specwidget'],
  982. to => '/specwidget',
  983. ) or return;
  984. is( $child->{flavor}, 'Strawberry', 'the custom field survived validation' );
  985. ok( !exists $child->{bogus_field}, 'an undeclared field was still stripped' );
  986. my $body = _series_page( 'spec_widget', 'specwidget', $title, 1 ) or return;
  987. # The payoff: this can only render if the wizard wrote spec_widget.json,
  988. # schema_for merged it over the base post schema, and validate() therefore
  989. # kept 'flavor' instead of deleting it as an unknown key.
  990. like( $body, qr{<span class="spec-flavor">Strawberry</span>}, 'the custom field rendered on the page' );
  991. like( $body, qr/Spec widget body\./, 'and the body came through the generated template' );
  992. };
  993. subtest 'a wizard-built type can depend on another type' => sub {
  994. # The type we will point at. The relation is what is under test, so keep
  995. # the target itself boring.
  996. my ( $code, $body, $err ) = _render(
  997. _admin(
  998. route => '/admin/wyzzerdd/save',
  999. method => 'POST',
  1000. name => 'spec_target',
  1001. body_form => 'form_common.tx',
  1002. wrapper => 1,
  1003. inc_post_title => 1,
  1004. inc_title_input => 1,
  1005. inc_visibility => 1,
  1006. inc_tags => 1,
  1007. display => q{<div><: render_it($post.data) | mark_raw :></div>},
  1008. ),
  1009. \&Trog::Routes::HTML::post_wizard_save,
  1010. );
  1011. is( $code, 200, 'target type created' ) or diag($err);
  1012. # ...and the type that depends on it: one picked by id, one pulling in the
  1013. # whole set.
  1014. ( $code, $body, $err ) = _render(
  1015. _admin(
  1016. route => '/admin/wyzzerdd/save',
  1017. method => 'POST',
  1018. name => 'spec_relator',
  1019. body_form => 'form_common.tx',
  1020. wrapper => 1,
  1021. inc_post_title => 1,
  1022. inc_title_input => 1,
  1023. inc_visibility => 1,
  1024. inc_tags => 1,
  1025. param_name => [ 'chosen', 'everything' ],
  1026. param_type => [ 'relation', 'relation' ],
  1027. param_label => [ 'Chosen One', 'All Of Them' ],
  1028. param_placeholder => [ '', '' ],
  1029. param_required => [ 0, 0 ],
  1030. param_relation_form => [ 'spec_target.tx', 'spec_target.tx' ],
  1031. param_relation_mode => [ 'one', 'all' ],
  1032. display => q{<div class="rel"><span class="chosen"><: $post.chosen_post.title :></span>} . q{<: for $post.everything -> $t { :><span class="each"><: $t.title :></span><: } :></div>},
  1033. ),
  1034. \&Trog::Routes::HTML::post_wizard_save,
  1035. );
  1036. is( $code, 200, 'relating type created' ) or diag($err);
  1037. my $sidecar = JSON::MaybeXS::decode_json( Path::Tiny->new('www/templates/html/components/forms/spec_relator.json')->slurp_utf8 );
  1038. # 'one' is a field the post stores; 'all' is not a field at all.
  1039. is( $sidecar->{properties}{chosen}{type}, 'relation', 'the picked relation is a stored property' );
  1040. is( $sidecar->{properties}{chosen}{'x-tcms-relation-form'}, 'spec_target.tx', 'naming its target type' );
  1041. ok( !exists $sidecar->{properties}{everything}, "the 'all' relation stores nothing" );
  1042. is_deeply(
  1043. $sidecar->{'x-tcms-relations'},
  1044. {
  1045. chosen_post => { form => 'spec_target.tx', from => 'chosen' },
  1046. everything => { form => 'spec_target.tx' },
  1047. },
  1048. 'and both are declared for resolution at render'
  1049. );
  1050. # The relation pseudo-type has to become a real one before validation, or
  1051. # every relation field would be stripped as an unknown type.
  1052. is(
  1053. Trog::DataModule::schema_for('spec_relator.tx')->{properties}{chosen}{type},
  1054. 'string', "'relation' expands to a real schema type"
  1055. );
  1056. _reindex();
  1057. # Now exercise it end to end.
  1058. _make_series( 'spec_target', 'spectarget', 'spec_target.tx', 'Spec Target Series', no_topbar => 1 ) or return;
  1059. my $target = _save_post(
  1060. 'spec_target child',
  1061. form => 'spec_target.tx',
  1062. title => 'Spec Target Post',
  1063. data => 'Spec target body.',
  1064. visibility => 'public',
  1065. tags => ['spectarget'],
  1066. to => '/spectarget',
  1067. ) or return;
  1068. _make_series( 'spec_relator', 'specrelator', 'spec_relator.tx', 'Spec Relator Series', no_topbar => 1 ) or return;
  1069. my $relator = _save_post(
  1070. 'spec_relator child',
  1071. form => 'spec_relator.tx',
  1072. title => 'Spec Relator Post',
  1073. data => 'Spec relator body.',
  1074. chosen => $target->{id},
  1075. visibility => 'public',
  1076. tags => ['specrelator'],
  1077. to => '/specrelator',
  1078. ) or return;
  1079. is( $relator->{chosen}, $target->{id}, 'the referenced id survived validation' );
  1080. my $page = _series_page( 'spec_relator', 'specrelator', 'Spec Relator Series', 1 ) or return;
  1081. # Both halves of the relation resolved: the one we picked, and the set.
  1082. like( $page, qr{<span class="chosen">Spec Target Post</span>}, 'the picked post resolved into the page' );
  1083. like( $page, qr{<span class="each">Spec Target Post</span>}, 'and so did the whole set' );
  1084. };
  1085. subtest '/api/posts_of_form feeds the relation picker' => sub {
  1086. my $target = _find_post('Spec Target Post');
  1087. ok( $target, 'the target post is there to be listed' ) or return;
  1088. my $res = Trog::Routes::JSON::posts_of_form( _admin( route => '/api/posts_of_form', form => 'spec_target.tx' ) );
  1089. is( ref $res, 'ARRAY', 'a PSGI response came back' ) or return;
  1090. my ( $code, undef, $body ) = @$res;
  1091. is( $code, 200, 'and it is a 200' );
  1092. my $payload = JSON::MaybeXS::decode_json( join( '', @$body ) );
  1093. is_deeply(
  1094. $payload->{posts},
  1095. [ { id => $target->{id}, title => 'Spec Target Post' } ],
  1096. 'listing exactly the posts of that type, id and title only'
  1097. );
  1098. # An unknown type is an empty list, not an error -- a type with no posts
  1099. # yet is the normal state of affairs right after you create it.
  1100. $res = Trog::Routes::JSON::posts_of_form( _admin( route => '/api/posts_of_form', form => 'nosuchtype.tx' ) );
  1101. $payload = JSON::MaybeXS::decode_json( join( '', @{ $res->[2] } ) );
  1102. is_deeply( $payload->{posts}, [], 'an unknown type lists nothing rather than failing' );
  1103. # The route's own declaration is what keeps a malformed form out; check it
  1104. # matches what series.json demands of child_form.
  1105. my $validator = $Trog::Routes::JSON::routes{'/api/posts_of_form'}{parameters}{form};
  1106. ok( $validator->('blog.tx'), 'the parameter validator accepts a form name' );
  1107. ok( !$validator->('../../etc/passwd'), 'and rejects a path' );
  1108. ok( $Trog::Routes::JSON::routes{'/api/posts_of_form'}{auth}, 'the route requires a login' );
  1109. };
  1110. subtest 'a series resolves its relations for logged out visitors too' => sub {
  1111. # The relations series() resolves land on the primary post, which is what a
  1112. # datasource reads to find its hypervisors -- so assert on that rather than
  1113. # on the rendered children, whose relations posts() resolves later, after
  1114. # it has widened user_acls and thus by a path that was never broken.
  1115. foreach my $case ( [ 'anonymously', _anon( route => '/specrelator' ) ], [ 'as an admin', _admin( route => '/specrelator' ) ] ) {
  1116. my ( $label, $query ) = @$case;
  1117. my ( $code, undef, $err ) = _render( $query, \&Trog::Routes::HTML::series );
  1118. is( $code, 200, "the series renders $label" ) or do { diag($err); next };
  1119. my $resolved = $query->{primary_post}{everything};
  1120. is( ref $resolved, 'ARRAY', "$label, the series' own relation resolved" );
  1121. cmp_ok( scalar @{ $resolved || [] }, '>', 0, "$label, to something rather than nothing" );
  1122. }
  1123. };
  1124. subtest 'saves report back through the jsalert banner' => sub {
  1125. # A good save redirects to where you came from, carrying the outcome.
  1126. my $res = Trog::Routes::HTML::post_save(
  1127. _admin(
  1128. route => '/post/save',
  1129. method => 'POST',
  1130. to => '/specblog',
  1131. form => 'blog.tx',
  1132. title => 'Spec Feedback Child',
  1133. data => 'Spec feedback body.',
  1134. visibility => 'public',
  1135. tags => ['specblog'],
  1136. )
  1137. );
  1138. my ( $code, $headers ) = @$res;
  1139. my %h = @{ $headers || [] };
  1140. is( $code, 303, 'a good save redirects' );
  1141. is( $h{Location}, '/secure/specblog', 'to where we came from, and nowhere else' );
  1142. # The outcome rides in a cookie, not the URL. It used to be a query
  1143. # parameter, and a save which failed with a stack trace attached built a URL
  1144. # of several thousand characters that tPSGI answered with a 419 -- so the
  1145. # user saw neither the page nor the error.
  1146. unlike( $h{Location}, qr/[?&]/, 'the URL carries no message at all' );
  1147. ok( length( $h{Location} ) < 2048, 'so it cannot outgrow what a server will accept' );
  1148. like( $h{'Set-Cookie'}, qr/^tcmsfeedback=0:/, 'the outcome is in a cookie instead' );
  1149. unlike( $h{'Set-Cookie'}, qr/\n/, 'and the message is one line, since it lands in a JS string' );
  1150. _reindex();
  1151. # Follow the redirect the way a browser would: same destination, carrying
  1152. # the cookie it was just handed.
  1153. my $feedback = ( split( /;/, $h{'Set-Cookie'} ) )[0];
  1154. my ( $rcode, $body, $err ) = _render(
  1155. _admin( route => '/specblog', cookies => $feedback ),
  1156. \&Trog::Routes::HTML::series,
  1157. );
  1158. is( $rcode, 200, 'the destination renders' ) or diag($err);
  1159. like( $body, qr/var loginFailure = 0;/, 'the banner is in success mode' );
  1160. like( $body, qr/Saved post &#39;Spec Feedback Child&#39;\./, 'and says what was saved' );
  1161. # Nothing to navigate on to, so the banner must not bounce us anywhere.
  1162. unlike( $body, qr/window\.location=/, 'and does not redirect onwards' );
  1163. # A rejected save comes back the same way, in red.
  1164. $res = Trog::Routes::HTML::post_save(
  1165. _admin(
  1166. route => '/post/save',
  1167. method => 'POST',
  1168. to => '/specblog',
  1169. form => 'blog.tx',
  1170. title => 'Spec Rejected Child',
  1171. visibility => 'not-a-visibility',
  1172. tags => ['specblog'],
  1173. )
  1174. );
  1175. ( $code, $headers ) = @$res;
  1176. %h = @{ $headers || [] };
  1177. is( $code, 303, 'a rejected save redirects too, rather than dead-ending on a 400' );
  1178. is( $h{Location}, '/secure/specblog', 'to the same place a good one goes' );
  1179. like( $h{'Set-Cookie'}, qr/^tcmsfeedback=1:/, 'flagged as failed' );
  1180. like( URI::Escape::uri_unescape( $h{'Set-Cookie'} ), qr/Not in enum list/, 'carrying the validator error' );
  1181. ok( !_find_post('Spec Rejected Child'), 'and the bad post was not written' );
  1182. $feedback = ( split( /;/, $h{'Set-Cookie'} ) )[0];
  1183. ( $rcode, $body, $err ) = _render(
  1184. _admin( route => '/specblog', cookies => $feedback ),
  1185. \&Trog::Routes::HTML::series,
  1186. );
  1187. is( $rcode, 200, 'the destination still renders' ) or diag($err);
  1188. like( $body, qr/var loginFailure = 1;/, 'the banner is in failure mode' );
  1189. like( $body, qr/Not in enum list/, 'and shows why' );
  1190. # Read once: the cookie is expired by the page that rendered it, or the
  1191. # banner follows the reader around until it times out on its own.
  1192. my ( undef, undef, undef, $rheaders ) = _render(
  1193. _admin( route => '/specblog', cookies => $feedback ),
  1194. \&Trog::Routes::HTML::series,
  1195. );
  1196. my %rh = @{ $rheaders || [] };
  1197. like( $rh{'Set-Cookie'}, qr/^tcmsfeedback=;.*Max-Age=0/, 'and the page that shows it expires it' );
  1198. # A page showing a banner must not be cached, or the one person who saved
  1199. # something has their message served to everybody. The query parameter
  1200. # form was accidentally safe here; a cookie is not.
  1201. my $tpsgi = Test::TPSGI->new( run_callbacks => 1 );
  1202. _render( _anon( route => '/specblog', cookies => $feedback, tpsgi => $tpsgi ), \&Trog::Routes::HTML::series );
  1203. is_deeply( [ keys %{ $tpsgi->{renders} } ], [], 'and it is never saved as a static' );
  1204. };
  1205. subtest 'a hidden id that was never filled in' => sub {
  1206. # Every editor posts <input type="hidden" name="id"> and it is empty for a
  1207. # new post. validate() keeps an empty string for a field the schema types
  1208. # as a string, which id is, so //= saw a defined value and left it -- and
  1209. # the flat file model wrote the post to 'data/files/', the datastore
  1210. # directory rather than a post in it. The error named File::Slurper, and
  1211. # its stack trace then went into the redirect URL and produced a 419.
  1212. my $res = Trog::Routes::HTML::post_save(
  1213. _admin(
  1214. route => '/post/save',
  1215. method => 'POST',
  1216. to => '/specblog',
  1217. id => '',
  1218. form => 'blog.tx',
  1219. title => 'Spec Empty Id Child',
  1220. data => 'Saved despite an empty hidden id.',
  1221. visibility => 'public',
  1222. tags => ['specblog'],
  1223. )
  1224. );
  1225. my ( $code, $headers ) = @$res;
  1226. my %h = @{ $headers || [] };
  1227. is( $code, 303, 'the save succeeds' );
  1228. like( $h{'Set-Cookie'}, qr/^tcmsfeedback=0:/, 'and reports success' );
  1229. _reindex();
  1230. my $saved = _find_post('Spec Empty Id Child');
  1231. ok( $saved, 'the post was written' ) or return;
  1232. ok( length( $saved->{id} // '' ), 'with an id of its own' );
  1233. isnt( $saved->{id}, '', 'rather than the empty one it was handed' );
  1234. # And the datastore directory is still a directory.
  1235. ok( -d 'data/files', 'the datastore was not written over' );
  1236. };
  1237. subtest 'the topbar did not silently overflow' => sub {
  1238. my ( $code, $body, $err ) = _render( _anon( route => '/' ) );
  1239. is( $code, 200, '/ renders' ) or diag($err);
  1240. my @titles = _topbar_titles($body);
  1241. # 2 seeded series (Series, About) + the 7 we built. _get_series() caps at
  1242. # 10 (HTML.pm), so an eighth post type would start silently dropping one.
  1243. is( scalar @titles, 9, 'every series we made is in the topbar' )
  1244. or diag( 'topbar was: ' . join( ', ', @titles ) );
  1245. cmp_ok( scalar @titles, '<=', 10, 'and we are not at the _get_series limit' );
  1246. };
  1247. subtest 'the tpsgi side effects stayed inert' => sub {
  1248. cmp_ok( $TPSGI->{restarts}, '>', 0, 'post_save did try to signal a restart' );
  1249. cmp_ok( $TPSGI->{callbacks}, '>', 0, 'and did queue post-close callbacks' );
  1250. # We never run the queued callbacks, which is what keeps save_render and
  1251. # invalidate_renders from ever firing.
  1252. is_deeply( $TPSGI->{renders}, {}, 'no static renders were written' );
  1253. is_deeply( $TPSGI->{invalidated}, [], 'and nothing was invalidated' );
  1254. my @statics = glob('www/statics/*');
  1255. is_deeply( \@statics, [], 'www/statics is empty' );
  1256. };
  1257. subtest 'the real repo was not touched' => sub {
  1258. my %after = map { _snapshot("$REPO/$_") } qw{config data www/templates www/assets logs totp};
  1259. my @changed = grep { ( $REPO_BEFORE{$_} // '' ) ne ( $after{$_} // '' ) } keys %after;
  1260. my @removed = grep { !exists $after{$_} } keys %REPO_BEFORE;
  1261. is_deeply( [ sort @changed ], [], 'no repo file was added or modified' );
  1262. is_deeply( [ sort @removed ], [], 'and none were removed' );
  1263. ok( !-e "$REPO/www/templates/html/components/forms/spec_widget.tx", 'the wizard type is not in the repo' );
  1264. };
  1265. done_testing();