fix has_tags([$names]) and add has_tag($name)/has_tag($tag_object)
authorTony Cook <tony@develop-help.com>
Thu, 20 Aug 2015 10:52:02 +0000 (20:52 +1000)
committerTony Cook <tony@develop-help.com>
Thu, 20 Aug 2015 10:52:02 +0000 (20:52 +1000)
site/cgi-bin/modules/BSE/TB/TagOwner.pm
t/050-local/030-tags.t

index 76ff930339ecef05e3a554376eb5be43f7b5a64b..37b8d41373580bd202dc7c30a80c8450308325eb 100644 (file)
@@ -6,7 +6,7 @@ use strict;
 use BSE::TB::Tags;
 use BSE::TB::TagMembers;
 
-our $VERSION = "1.004";
+our $VERSION = "1.005";
 
 =head1 NAME
 
@@ -171,6 +171,8 @@ sub tag_members {
 
 Check that all of the specified tags are on the object.
 
+The array can contain either tag names or tag objects.
+
 =cut
 
 sub has_tags {
@@ -187,10 +189,39 @@ sub has_tags {
        or return;
     }
 
-    $my_tag_ids{$tag->id}
+    $my_tag_ids{$work->id}
+      or return;
+  }
+
+  return 1;
+}
+
+=item has_tag($tag_name)
+
+=item has_tag($tag_object)
+
+Return true if the given tag can be found on the object.
+
+=cut
+
+sub has_tag {
+  my ($self, $tag) = @_;
+
+  unless (ref $tag) {
+    $tag = BSE::TB::Tags->getByName($self->tag_owner_type, $tag)
       or return;
   }
 
+  my @members = BSE::TB::TagMembers->getBy
+    (
+     owner_id => $self->id,
+     owner_type => $self->tag_owner_type,
+     tag_id => $tag->id,
+    );
+
+  @members
+    or return;
+
   return 1;
 }
 
index 09f5dbf30ad8bd0321296169f12b5a43b9573b50..2a0bd8f9213f76ade0bdc7328da8fffa997091d0 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 use strict;
 use BSE::Test ();
-use Test::More tests => 38;
+use Test::More tests => 51;
 use File::Spec;
 use Carp qw(confess);
 
@@ -226,6 +226,29 @@ is_deeply([ map $_->title, $parent->children ],
   }
 }
 
+{
+  my $error;
+  ok($parent->set_tags([ "SomeTag" ], \$error), "add SomeTag to parent");
+  ok($parent->has_tags([ "SomeTag" ]), "check true has_tags() by name");
+  my ($tag) = $parent->tag_objects;
+  is($tag->name, "SomeTag", "check tag_objects tag");
+  ok($parent->has_tags([ $tag ]), "check true has_tags() by object");
+  ok(!$parent->has_tags([ "Platform: iPad" ]), "check false has_tags() by name");
+  my $unknown = "Unknown: " . time();
+  ok(!$parent->has_tags([ $unknown ]),
+     "check false has_tags() by name (unknown tag)");
+  is_deeply([ $parent->tags ], [ "SomeTag" ], "tags() method");
+  my ($tag_id) = $parent->tag_ids;
+  is($tag_id, $tag->id, "check tag_ids");
+  my $by_name = $parent->tag_by_name("SomeTag");
+  is($by_name->id, $tag->id, "check tag_by_name()");
+  ok($parent->has_tag("SomeTag"), "check true has_tag() by name");
+  ok($parent->has_tag($tag), "check true has_tag() by object");
+  ok(!$parent->has_tag("Platform: iPad"), "check false has_tag() by name");
+  ok(!$parent->has_tag($unknown),
+     "check false has_tag() by name (unknown tag)");
+}
+
 END {
   for my $kid (@kids) {
     $kid->remove($cfg);