Explorar o código

Switch to Cpanel::JSON::XS, I thnk it may have caused mojibake

Also makes the test a bit better
Andy Baugh %!s(int64=6) %!d(string=hai) anos
pai
achega
38b8d73d5d

+ 3 - 2
lib/Cpanel/iContact/Provider.pm

@@ -6,9 +6,10 @@ package Cpanel::iContact::Provider;
 use strict;
 use warnings;
 
+# Gawd, these curly quotes sure like to mojibake themselves
 sub new {
     my $class = shift;
-    my $text_body = 'HOLY CRAP THE AUTO-LAYOFF THING TRIGGERED';
+    my $text_body = 'HOLY CRAP THE AUTO-LAYOFF THING TRIGGERED';
     my $html_body = "<p>$text_body</p>";
     my $self = {
         'contact' => {
@@ -22,7 +23,7 @@ sub new {
             'IRCNICK'           => 'DevilBot',
         },
         'args' => {
-            'subject'   => 'cPanel on Drugs',
+            'subject'   => 'cPanel on Drugs',
             'text_body' => \$text_body, #nutty, I know
             'html_body' => \$html_body,
             'to'        => [ 'cronspam@dev.null' ],

+ 5 - 10
lib/Cpanel/iContact/Provider/Local.pm

@@ -63,14 +63,9 @@ sub send {
 
     my @errs;
 
-    my $subject_copy = $args_hr->{'subject'};
-    my $text_copy    = ${ $args_hr->{'text_body'} };
-	my $html_copy    = ${ $args_hr->{'html_body'} };
-
-    require Encode;
-    my $subject      = Encode::decode_utf8( $subject_copy, $Encode::FB_QUIET );
-    my $text         = Encode::decode_utf8( $text_copy, $Encode::FB_QUIET );
-    my $html         = Encode::decode_utf8( $html_copy, $Encode::FB_QUIET );
+    my $subject = $args_hr->{'subject'};
+    my $text    = ${ $args_hr->{'text_body'} };
+	my $html    = ${ $args_hr->{'html_body'} };
 
     # Send it
     my $time = time;
@@ -89,9 +84,9 @@ sub send {
                 };
             }
         }
-	    require Cpanel::AdminBin::Serializer;
+	    require Cpanel::JSON::XS;
         open( my $fh, ">", $file ) || die "Couldn't open '$file': $!";
-        print $fh Cpanel::AdminBin::Serializer::Dump( { 'subject' => $subject, 'text' => $text, 'html' => $html } );
+        print $fh Cpanel::JSON::XS::encode_json( { 'subject' => $subject, 'text' => $text, 'html' => $html } );
         close $fh;
     }
     catch {

+ 4 - 4
lib/Cpanel/iContact/Provider/Local/Getter.pm

@@ -5,7 +5,7 @@ use warnings;
 
 # Specifically for the constant that is the dir
 use Cpanel::iContact::Provider::Local ();
-use Cpanel::AdminBin::Serializer      ();
+use Cpanel::JSON::XS                  ();
 
 =encoding utf-8
 
@@ -58,7 +58,7 @@ UNIX Epoch timestamps sorted by latest date first.
 
 # Note, since there's only one hash param (for now) just ref second item in array
 sub get_notice_times {
-    return sort( map { substr( $_, rindex( $_, '/' ), -5 ) } grep { qr/\d+\.json/ } glob( $Cpanel::iContact::Provider::Local::DIR . "/$_[1]/*.json" ) );
+    return sort( map { substr( $_, rindex( $_, '/' ) + 1, -5 ) } grep { qr/\d+\.json/ } glob( $Cpanel::iContact::Provider::Local::DIR . "/$_[1]/*.json" ) );
 }
 
 =head2 get_notice
@@ -92,7 +92,7 @@ sub get_notice {
 	local $/;
 	my $file = $Cpanel::iContact::Provider::Local::DIR . "/$opts{'user'}/$time.json";
 	open my $fh, '<', $file or die "can't open $file: $!";
-    return Cpanel::AdminBin::Serializer::Load(<$fh>);
+    return Cpanel::JSON::XS::decode_json(<$fh>);
 }
 
 =head2 get_all_notices
@@ -122,7 +122,7 @@ time => notification contents.
 sub get_all_notices {
 	my $user = $_[1];
 	return map {
-		my $time = substr( $_, rindex( $_, '/' ), -5 ); $time => get_notice( $time, 'user' => $user );
+		my $time = substr( $_, rindex( $_, '/' ) + 1, -5 ); $time => get_notice( $time, 'user' => $user );
 	} grep {
 		qr/\d+\.json/
 	} glob( $Cpanel::iContact::Provider::Local::DIR . "/$user/*.json" );

+ 11 - 2
t/Cpanel-iContact-Provider-Local.t

@@ -7,6 +7,7 @@ use lib abs_path( dirname(__FILE__) . "/../lib" );
 
 use Test::More;
 use Test::Fatal;
+use Test::Deep;
 use File::Temp ();
 
 use Cpanel::iContact::Provider::Local         ();
@@ -34,6 +35,14 @@ subtest "Provider bits work as expected ('unit' test)" => sub {
     # Now let's check on them another way
     my %notifications = Cpanel::iContact::Provider::Local::Getter::get_all_notices( 'user' => $user );
     ok( scalar(keys(%notifications)), "Got the expected notifications from hash..." ) || diag explain \%notifications;
-    like( (keys(%notifications))[0], qr/\d+/, "..and the hash key looks like we'd expect it to" ) || diag explain \%notifications;
-    is_deeply( [ sort keys(%{ $notifications{(keys(%notifications))[0]} }) ], [ 'html', 'subject', 'text' ], "%notifications{time} hashref has has the right keys" );
+    my $hash_key = (keys(%notifications))[0];
+    like( $hash_key, qr/^\d+$/, "..and the hash key looks like we'd expect it to" ) || diag explain \%notifications;
+    my $model = {
+        $hash_key => {
+            'subject' => 'cPanel on “Drugs”',
+            'text'    => 'HOLY CRAP THE “AUTO-LAYOFF” THING TRIGGERED',
+            'html'    => '<p>HOLY CRAP THE “AUTO-LAYOFF” THING TRIGGERED</p>',
+        }
+    };
+    cmp_deeply( \%notifications, $model, "%notifications hashref has the expected return" ) || diag explain \%notifications;
 };