From: Peter Rabbitson Date: Tue, 14 Dec 2010 12:35:34 +0000 (+0100) Subject: Sanity check args passed to a coderef conditionmaker X-Git-Tag: v0.08190~1^2~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6fbef4a4db1f453046b0450c485c495d743030d5;p=dbsrgits%2FDBIx-Class.git Sanity check args passed to a coderef conditionmaker --- diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 050c056..41a8fec 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -586,10 +586,13 @@ sub create_related { # just forbid it right now. my $rel_info = $self->result_source->relationship_info($rel); if (ref $rel_info->{cond} eq 'CODE') { - my ($cond, $ext) = $rel_info->{cond}->({ self_alias => 'me', - foreign_alias => $rel, - self_rowobj => $self - }); + my ($cond, $ext) = $rel_info->{cond}->({ + self_alias => 'me', + foreign_alias => $rel, + self_rowobj => $self, + self_resultsource => $self->result_source, + foreign_relname => $rel, + }); $self->throw_exception("unable to set_from_related - no simplified condition available for '${rel}'") unless $ext; diff --git a/t/lib/DBICTest/Schema/Artist.pm b/t/lib/DBICTest/Schema/Artist.pm index e86aae0..e1556ae 100644 --- a/t/lib/DBICTest/Schema/Artist.pm +++ b/t/lib/DBICTest/Schema/Artist.pm @@ -2,6 +2,7 @@ package # hide from PAUSE DBICTest::Schema::Artist; use base qw/DBICTest::BaseResult/; +use Carp qw/confess/; __PACKAGE__->table('artist'); __PACKAGE__->source_info({ @@ -51,6 +52,14 @@ __PACKAGE__->has_many( sub { my $args = shift; + # This is for test purposes only. A regular user does not + # need to sanity check the passed-in arguments, this is what + # the tests are for :) + my @missing_args = grep { ! defined $args->{$_} } + qw/self_alias foreign_alias self_resultsource foreign_relname/; + confess "Required arguments not supplied to custom rel coderef: @missing_args\n" + if @missing_args; + return ( { "$args->{foreign_alias}.artist" => { '=' => { -ident => "$args->{self_alias}.artistid"} }, "$args->{foreign_alias}.year" => { '>' => 1979, '<' => 1990 }, @@ -68,6 +77,15 @@ __PACKAGE__->has_many( cds_84 => 'DBICTest::Schema::CD', sub { my $args = shift; + + # This is for test purposes only. A regular user does not + # need to sanity check the passed-in arguments, this is what + # the tests are for :) + my @missing_args = grep { ! defined $args->{$_} } + qw/self_alias foreign_alias self_resultsource foreign_relname/; + confess "Required arguments not supplied to custom rel coderef: @missing_args\n" + if @missing_args; + return ( { "$args->{foreign_alias}.artist" => { -ident => "$args->{self_alias}.artistid" }, "$args->{foreign_alias}.year" => 1984, @@ -85,6 +103,15 @@ __PACKAGE__->has_many( cds_90s => 'DBICTest::Schema::CD', sub { my $args = shift; + + # This is for test purposes only. A regular user does not + # need to sanity check the passed-in arguments, this is what + # the tests are for :) + my @missing_args = grep { ! defined $args->{$_} } + qw/self_alias foreign_alias self_resultsource foreign_relname/; + confess "Required arguments not supplied to custom rel coderef: @missing_args\n" + if @missing_args; + return ( { "$args->{foreign_alias}.artist" => { -ident => "$args->{self_alias}.artistid" }, "$args->{foreign_alias}.year" => { '>' => 1989, '<' => 2000 }, diff --git a/t/lib/DBICTest/Schema/Artwork.pm b/t/lib/DBICTest/Schema/Artwork.pm index 6aa8df9..351d9dd 100644 --- a/t/lib/DBICTest/Schema/Artwork.pm +++ b/t/lib/DBICTest/Schema/Artwork.pm @@ -2,6 +2,7 @@ package # hide from PAUSE DBICTest::Schema::Artwork; use base qw/DBICTest::BaseResult/; +use Carp qw/confess/; __PACKAGE__->table('cd_artwork'); __PACKAGE__->add_columns( @@ -23,16 +24,26 @@ __PACKAGE__->many_to_many('artists_test_m2m_noopt', 'artwork_to_artist', 'artist # other test to manytomany __PACKAGE__->has_many('artwork_to_artist_test_m2m', 'DBICTest::Schema::Artwork_to_Artist', - sub { - my $args = shift; - return ( - { "$args->{foreign_alias}.artwork_cd_id" => { -ident => "$args->{self_alias}.cd_id" }, - }, - $args->{self_rowobj} && { - "$args->{foreign_alias}.artwork_cd_id" => $args->{self_rowobj}->cd_id, - } - ); - }); + sub { + my $args = shift; + + # This is for test purposes only. A regular user does not + # need to sanity check the passed-in arguments, this is what + # the tests are for :) + my @missing_args = grep { ! defined $args->{$_} } + qw/self_alias foreign_alias self_resultsource foreign_relname/; + confess "Required arguments not supplied to custom rel coderef: @missing_args\n" + if @missing_args; + + return ( + { "$args->{foreign_alias}.artwork_cd_id" => { -ident => "$args->{self_alias}.cd_id" }, + }, + $args->{self_rowobj} && { + "$args->{foreign_alias}.artwork_cd_id" => $args->{self_rowobj}->cd_id, + } + ); + } +); __PACKAGE__->many_to_many('artists_test_m2m2', 'artwork_to_artist_test_m2m', 'artist'); 1; diff --git a/t/lib/DBICTest/Schema/Artwork_to_Artist.pm b/t/lib/DBICTest/Schema/Artwork_to_Artist.pm index 7cb25ca..dc0d50d 100644 --- a/t/lib/DBICTest/Schema/Artwork_to_Artist.pm +++ b/t/lib/DBICTest/Schema/Artwork_to_Artist.pm @@ -2,6 +2,7 @@ package # hide from PAUSE DBICTest::Schema::Artwork_to_Artist; use base qw/DBICTest::BaseResult/; +use Carp qw/confess/; __PACKAGE__->table('artwork_to_artist'); __PACKAGE__->add_columns( @@ -19,27 +20,47 @@ __PACKAGE__->belongs_to('artwork', 'DBICTest::Schema::Artwork', 'artwork_cd_id') __PACKAGE__->belongs_to('artist', 'DBICTest::Schema::Artist', 'artist_id'); __PACKAGE__->belongs_to('artist_test_m2m', 'DBICTest::Schema::Artist', - sub { - my $args = shift; - return ( - { "$args->{foreign_alias}.artistid" => { -ident => "$args->{self_alias}.artist_id" }, - "$args->{foreign_alias}.rank" => { '<' => 10 }, - }, - $args->{self_rowobj} && { - "$args->{foreign_alias}.artistid" => $args->{self_rowobj}->artist_id, - "$args->{foreign_alias}.rank" => { '<' => 10 }, - } - ); - }); + sub { + my $args = shift; + + # This is for test purposes only. A regular user does not + # need to sanity check the passed-in arguments, this is what + # the tests are for :) + my @missing_args = grep { ! defined $args->{$_} } + qw/self_alias foreign_alias self_resultsource foreign_relname/; + confess "Required arguments not supplied to custom rel coderef: @missing_args\n" + if @missing_args; + + return ( + { "$args->{foreign_alias}.artistid" => { -ident => "$args->{self_alias}.artist_id" }, + "$args->{foreign_alias}.rank" => { '<' => 10 }, + }, + $args->{self_rowobj} && { + "$args->{foreign_alias}.artistid" => $args->{self_rowobj}->artist_id, + "$args->{foreign_alias}.rank" => { '<' => 10 }, + } + ); + } +); __PACKAGE__->belongs_to('artist_test_m2m_noopt', 'DBICTest::Schema::Artist', - sub { - my $args = shift; - return ( - { "$args->{foreign_alias}.artistid" => { -ident => "$args->{self_alias}.artist_id" }, - "$args->{foreign_alias}.rank" => { '<' => 10 }, - } - ); - }); + sub { + my $args = shift; + + # This is for test purposes only. A regular user does not + # need to sanity check the passed-in arguments, this is what + # the tests are for :) + my @missing_args = grep { ! defined $args->{$_} } + qw/self_alias foreign_alias self_resultsource foreign_relname/; + confess "Required arguments not supplied to custom rel coderef: @missing_args\n" + if @missing_args; + + return ( + { "$args->{foreign_alias}.artistid" => { -ident => "$args->{self_alias}.artist_id" }, + "$args->{foreign_alias}.rank" => { '<' => 10 }, + } + ); + } +); 1; diff --git a/t/lib/DBICTest/Schema/Track.pm b/t/lib/DBICTest/Schema/Track.pm index fb9e5c5..a57fcb5 100644 --- a/t/lib/DBICTest/Schema/Track.pm +++ b/t/lib/DBICTest/Schema/Track.pm @@ -2,6 +2,8 @@ package # hide from PAUSE DBICTest::Schema::Track; use base qw/DBICTest::BaseResult/; +use Carp qw/confess/; + __PACKAGE__->load_components(qw/InflateColumn::DateTime Ordered/); __PACKAGE__->table('track'); @@ -67,6 +69,15 @@ __PACKAGE__->might_have ( next_track => __PACKAGE__, sub { my $args = shift; + + # This is for test purposes only. A regular user does not + # need to sanity check the passed-in arguments, this is what + # the tests are for :) + my @missing_args = grep { ! defined $args->{$_} } + qw/self_alias foreign_alias self_resultsource foreign_relname/; + confess "Required arguments not supplied to custom rel coderef: @missing_args\n" + if @missing_args; + return ( { "$args->{foreign_alias}.cd" => { -ident => "$args->{self_alias}.cd" }, "$args->{foreign_alias}.position" => { '>' => { -ident => "$args->{self_alias}.position" } },