root/branches/release-31/t/21-callbacks.t @ 1459

Revision 1459, 4.1 kB (checked in by mpaschal, 21 months ago)

spacing
confirm callbacks were called with explicit variables too
fully comment out old-school object driver mucking
category_id fields is now category_ids
use new technique for calling an app's mode
confirm save_entry succeeded (with redirect) after we called it

  • Property svn:mime-type set to text/plain
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl
2# $Id$
3use strict;
4use warnings;
5
6use Test::More tests => 8;
7use CGI;
8use DB_File;
9
10use MT;
11use MT::Plugin;
12use MT::Entry;
13use MT::App::CMS;
14use MT::Permission;
15
16use vars qw($T_CFG);
17
18use lib 't';
19require 'test-common.pl';
20require 'blog-common.pl';
21
22my $mt = MT->new(Config => $T_CFG);
23die "Couldn't create MT (" . MT->errstr. ")" unless $mt;
24
25sub rot13 {
26    $_[0] =~ tr/A-Za-z/N-ZA-Mn-za-m/;
27    return $_[0];
28}
29
30my $plug = MT::Plugin->new();
31
32# my $blog = MT::Blog->new();
33# $blog->set_values({ name => 'none'});
34# $blog->save();
35
36my $plugin = MT::Plugin->new({name => "21-callbacks.t"});
37
38### Test object callbacks
39
40my ($pre_save_called, $post_load_called);
41MT->add_callback('MT::Entry::pre_save', 1, $plugin,
42                 sub { my ($eh, $obj, $app_obj) = @_;
43                       $pre_save_called = 1;
44                       $obj->text(rot13($obj->text));
45                       $app_obj->text($app_obj->text . '(rot13d)')} )
46    || die "Couldn't add pre_save cb: " . MT->errstr;
47MT->add_callback('MT::Entry::post_load', 1, $plugin,
48                 sub { my ($eh, $obj) = @_;
49                       $post_load_called = 1;
50                       $obj->text(rot13($obj->text)) } )
51    || die "Couldn't add post_load cb: " . MT->errstr;
52
53my $entry = MT::Entry->new();
54my $TEST_TEXT = "Come flow on the mic with the mighty mic master ";
55my $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.";
56$entry->author_id(1);
57$entry->status(1);
58$entry->text($TEST_TEXT);
59$entry->text_more($TEST_TEXT_MORE);
60$entry->title("Cantaloop");
61$entry->blog_id(1);
62$entry->save() or die $entry->errstr();
63
64ok($pre_save_called, 'pre-save callback was called');
65
66is($entry->text, $TEST_TEXT . '(rot13d)', 'in-mem object altered');
67
68my $id = $entry->id();
69ok($id, 'new entry has an id');
70
71my $entry2 = MT::Entry->load($id);
72ok($post_load_called, 'post-load callback was called');
73is($entry2->text, $TEST_TEXT, 'on-disk obj altered');
74
75=pod
76
77# TBD: generalize this
78my $driver = MT::ObjectDriver->new('DBI::SQLite');
79
80#my %entries;
81#tie %entries, "DB_File", $mt->{cfg}->DataSource . "/entry.db",
82#                         O_RDWR, 0400, $DB_BTREE
83#    || die $!;
84#my $rec = $entries{$id};
85#$rec = $driver->{serializer}->unserialize($rec);
86#is($$rec->{text}, rot13($TEST_TEXT), 'text rotated');
87
88#is($entry2->text_more, $TEST_TEXT_MORE, '$entry2->text_more()');
89#is($$rec->{text_more}, $TEST_TEXT_MORE, '$$rec->{text_more}');
90
91=cut
92
93### Test app callbacks
94
95my @result_cats = ();
96
97my $cms = MT::App::CMS->new(Config => $T_CFG);
98
99my $app_post_save_called;
100MT->add_callback('AppPostEntrySave', 1, $plugin,
101                 sub {
102                       $app_post_save_called = 1;
103                       my @plcmts = MT::Placement->load({entry_id => $_[2]->id});
104                       for my $plcmt (@plcmts) {
105                           push @result_cats, $plcmt->category_id;
106                       }
107                   } );
108
109MT::unplug();
110my $q = CGI->new();
111#$q->param(id => $entry2->id);
112$q->param(blog_id => $entry2->blog_id);
113$q->param(category_ids => 17);
114# $q->param(username => 'Chuck D');
115# $q->param(password => 'bass');
116$q->param(text => "Buddha blessed and boo-ya blasted;
117these are the words that she manifested.");
118$cms->{query} = $q;
119$cms->{perms} = MT::Permission->new();
120$cms->{perms}->can_post(1);
121$cms->{author} = MT::Author->new();
122$cms->{author}->name("Mel E. Mel");
123$cms->{author}->id(1);
124
125# fake out the magic; we're not testing that right now
126no warnings qw(once redefine);
127*MT::App::CMS::validate_magic = sub { 1; };
128use warnings qw(once redefine);
129
130my $handler = $cms->handler_to_coderef( $cms->handlers_for_mode('save_entry') );
131my $ret = $handler->($cms);
132ok(!defined $ret && $cms->{redirect}, 'entry save was successful');
133diag('Error: ' . $cms->errstr) if !defined $ret && !$cms->{redirect};
134
135ok($app_post_save_called, 'AppPostEntrySave callback was called');
136is($result_cats[0], 17, 'result_cats = 17');
137
Note: See TracBrowser for help on using the browser.