1use strict; 2use warnings; 3use Test::More; 4use lib 't/lib'; 5 6use_ok( 'HTML::FormHandler' ); 7 8use_ok( 'BookDB::Form::Book'); 9 10use_ok( 'BookDB::Schema'); 11 12my $schema = BookDB::Schema->connect('dbi:SQLite:t/db/book.db'); 13ok($schema, 'get db schema'); 14 15my $item = $schema->resultset('Book')->new_result({}); 16my $form = BookDB::Form::Book->new; 17 18ok( !$form->process( item => $item ), 'Empty data' ); 19 20# check authors options 21my $author_options = $form->field('authors')->options; 22is( $author_options->[0]->{label}, 'J.K. Rowling', 'right author name'); 23 24my $borrower_options = $form->field('borrower')->options; 25is( $borrower_options->[1]->{label}, 'John Doe <john@gmail.com>', 'right borrower name'); 26 27# This is munging up the equivalent of param data from a form 28my $good = { 29 'title' => 'How to Test Perl Form Processors', 30 'authors' => [5], 31 'genres' => [2, 4], 32 'format' => 2, 33 'isbn' => '123-02345-0502-2' , 34 'publisher' => 'EreWhon Publishing', 35 'user_updated' => 1, 36 'comment' => 'this is a comment', 37 'borrower' => undef, 38}; 39 40ok( $form->process( item => $item, params => $good ), 'Good data' ); 41 42my $book = $form->item; 43END { $book->delete }; 44 45ok ($book, 'get book object from form'); 46 47is( $book->extra, 'this is a comment', 'comment exists' ); 48is_deeply( $form->values, $good, 'values correct' ); 49$good->{$_} = '' for qw/ year pages borrower/; 50is_deeply( $form->fif, $good, 'fif correct' ); 51 52my $num_genres = $book->genres->count; 53is( $num_genres, 2, 'multiple select list updated ok'); 54 55is( $form->field('format')->value, 2, 'get value for format' ); 56 57$good->{genres} = 2; 58ok( $form->process($good), 'handle one value for multiple select' ); 59is_deeply( $form->field('genres')->value, [2], 'right value for genres' ); 60 61my $id = $book->id; 62 63$good->{authors} = []; 64$good->{genres} = [2,4]; 65$form->process($good); 66 67is_deeply( $form->field('authors')->value, [], 'author value right in form'); 68is( $form->field('publisher')->value, 'EreWhon Publishing', 'right publisher'); 69 70my $value_hash = { %{$good}, 71 authors => [], 72 year => undef, 73 pages => undef, 74 borrower => undef, 75 }; 76delete $value_hash->{submit}; 77is_deeply( $form->values, $value_hash, 'get right values from form'); 78 79my $bad_1 = { 80 notitle => 'not req', 81 silly_field => 4, 82}; 83 84ok( !$form->process( $bad_1 ), 'bad 1' ); 85 86$form = BookDB::Form::Book->new(item => $book, schema => $schema); 87ok( $form, 'create form from db object'); 88 89my $genres_field = $form->field('genres'); 90is_deeply( sort $genres_field->value, [2, 4], 'value of multiple field is correct'); 91 92my $bad_2 = { 93 'title' => "Another Silly Test Book", 94 'authors' => [6], 95 'year' => '1590', 96 'pages' => 'too few', 97 'format' => '22', 98}; 99 100ok( !$form->process( $bad_2 ), 'bad 2'); 101ok( $form->field('year')->has_errors, 'year has error' ); 102ok( $form->field('pages')->has_errors, 'pages has error' ); 103ok( !$form->field('authors')->has_errors, 'author has no error' ); 104ok( $form->field('format')->has_errors, 'format has error' ); 105 106my $values = $form->value; 107$values->{year} = 1999; 108$values->{pages} = 101; 109$values->{format} = 2; 110my $validated = $form->validate( $values ); 111ok( $validated, 'now form validates' ); 112 113$form->process; 114is( $book->publisher, 'EreWhon Publishing', 'publisher has not changed'); 115 116# test that multiple fields (genres) with value of [] deletes genres 117is( $book->genres->count, 2, 'multiple select list updated ok'); 118$good->{genres} = []; 119$form->process( $good ); 120is( $book->genres->count, 0, 'multiple select list has no selected options'); 121 122$form = BookDB::Form::Book->new(schema => $schema, active_column => 'is_active'); 123is( $form->field( 'genres' )->num_options, 3, 'active_column test' ); 124 125{ 126 package Test::Book; 127 use HTML::FormHandler::Moose; 128 extends 'HTML::FormHandler::Model::DBIC'; 129 130 has_field 'title' => ( minlength => 3, maxlength => 40, required => 1 ); 131 has_field 'year'; 132 has_field 'submit' => ( type => 'Submit' ); 133} 134 135# this tests to make sure that result loaded from db object is cleared when 136# the result is then loaded from the params 137$form = Test::Book->new; 138my $new_book = $schema->resultset('Book')->new_result({}); 139$form->process( item => $new_book, params => {} ); 140$form->process( item => $new_book, params => { title => 'abc' } ); 141is( $form->result->num_results, 3, 'right number of results'); 142 143done_testing; 144