forked from movabletype/movabletype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21-callbacks.t
78 lines (60 loc) · 2.21 KB
/
21-callbacks.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
# $Id: 21-callbacks.t 2562 2008-06-12 05:12:23Z bchoate $
use strict;
use warnings;
use Test::More tests => 5;
use CGI;
use lib 'extlib';
use lib 't/lib';
use lib 'lib';
use MT;
use MT::Test;
use MT::Plugin;
use MT::Entry;
use MT::App::CMS;
use MT::Permission;
use vars qw($T_CFG);
use lib 't';
require 'test-common.pl';
require 'blog-common.pl';
my $mt = MT->new(Config => $T_CFG);
die "Couldn't create MT (" . MT->errstr. ")" unless $mt;
sub rot13 {
$_[0] =~ tr/A-Za-z/N-ZA-Mn-za-m/;
return $_[0];
}
my $plug = MT::Plugin->new();
# my $blog = MT::Blog->new();
# $blog->set_values({ name => 'none'});
# $blog->save();
my $plugin = MT::Plugin->new({name => "21-callbacks.t"});
### Test object callbacks
my ($pre_save_called, $post_load_called);
MT->add_callback('MT::Entry::pre_save', 1, $plugin,
sub { my ($eh, $obj, $app_obj) = @_;
$pre_save_called = 1;
$obj->text(rot13($obj->text));
$app_obj->text($app_obj->text . '(rot13d)')} )
|| die "Couldn't add pre_save cb: " . MT->errstr;
MT->add_callback('MT::Entry::post_load', 1, $plugin,
sub { my ($eh, $obj) = @_;
$post_load_called = 1;
$obj->text(rot13($obj->text)) } )
|| die "Couldn't add post_load cb: " . MT->errstr;
my $entry = MT::Entry->new();
my $TEST_TEXT = "Come flow on the mic with the mighty mic master ";
my $TEST_TEXT_MORE = "beau-coup ducks, I'm a sucker to disaster. Scribble-dabbble scrabble, on the microphone I babble, as I fit the funky words! Into a puzzle! Yes, yes, yes, on and on as I flex, .... birds manifest, feel the vibe from here to Asia--dip trip, flip fantasia.";
$entry->author_id(1);
$entry->status(1);
$entry->text($TEST_TEXT);
$entry->text_more($TEST_TEXT_MORE);
$entry->title("Cantaloop");
$entry->blog_id(1);
$entry->save() or die $entry->errstr();
ok($pre_save_called, 'pre-save callback was called');
is($entry->text, $TEST_TEXT . '(rot13d)', 'in-mem object altered');
my $id = $entry->id();
ok($id, 'new entry has an id');
my $entry2 = MT::Entry->load($id);
ok($post_load_called, 'post-load callback was called');
is($entry2->text, $TEST_TEXT, 'on-disk obj altered');