Switch the bleadcpan test to 5.8.7
[dbsrgits/DBIx-Class.git] / t / 85utf8.t
CommitLineData
70350518 1use strict;
55087b99 2use warnings;
e59c17fe 3
70350518 4use Test::More;
d38cd95c 5use Test::Warn;
70350518 6use lib qw(t/lib);
7use DBICTest;
5cfb131d 8use DBIC::DebugObj;
e59c17fe 9
72ae8e40 10{
11 package A::Comp;
12 use base 'DBIx::Class';
13 sub store_column { shift->next::method (@_) };
14 1;
15}
16
17{
18 package A::SubComp;
19 use base 'A::Comp';
1415f198 20
72ae8e40 21 1;
22}
7146f619 23
72ae8e40 24warnings_are (
25 sub {
3c2a505c 26 local $ENV{DBIC_UTF8COLUMNS_OK} = 1;
1415f198 27 package A::Test1;
72ae8e40 28 use base 'DBIx::Class::Core';
29 __PACKAGE__->load_components(qw(Core +A::Comp Ordered UTF8Columns));
1415f198 30 __PACKAGE__->load_components(qw(Ordered +A::SubComp Row UTF8Columns Core));
31 sub store_column { shift->next::method (@_) };
72ae8e40 32 1;
33 },
34 [],
35 'no spurious warnings issued',
36);
37
70c28808 38warnings_like (
39 sub {
eed5492f 40 local $ENV{DBIC_UTF8COLUMNS_OK};
70c28808 41 package A::Test1Loud;
42 use base 'DBIx::Class::Core';
43 __PACKAGE__->load_components(qw(Core +A::Comp Ordered UTF8Columns));
44 __PACKAGE__->load_components(qw(Ordered +A::SubComp Row UTF8Columns Core));
45 sub store_column { shift->next::method (@_) };
46 1;
47 },
48 [qr/Use of DBIx::Class::UTF8Columns is strongly discouraged/],
49 'issued deprecation warning',
50);
51
52
1415f198 53my $test1_mro;
72ae8e40 54my $idx = 0;
1415f198 55for (@{mro::get_linear_isa ('A::Test1')} ) {
56 $test1_mro->{$_} = $idx++;
72ae8e40 57}
58
1415f198 59cmp_ok ($test1_mro->{'A::SubComp'}, '<', $test1_mro->{'A::Comp'}, 'mro of Test1 correct (A::SubComp before A::Comp)' );
60cmp_ok ($test1_mro->{'A::Comp'}, '<', $test1_mro->{'DBIx::Class::UTF8Columns'}, 'mro of Test1 correct (A::Comp before UTF8Col)' );
61cmp_ok ($test1_mro->{'DBIx::Class::UTF8Columns'}, '<', $test1_mro->{'DBIx::Class::Core'}, 'mro of Test1 correct (UTF8Col before Core)' );
62cmp_ok ($test1_mro->{'DBIx::Class::Core'}, '<', $test1_mro->{'DBIx::Class::Row'}, 'mro of Test1 correct (Core before Row)' );
63
1415f198 64warnings_like (
65 sub {
66 package A::Test2;
67 use base 'DBIx::Class::Core';
68 __PACKAGE__->load_components(qw(UTF8Columns +A::Comp));
69 sub store_column { shift->next::method (@_) };
70 1;
71 },
72 [qr/Incorrect loading order of DBIx::Class::UTF8Columns.+affect other components overriding 'store_column' \(A::Comp\)/],
73 'incorrect order warning issued (violator defines)',
74);
75
76warnings_like (
77 sub {
78 package A::Test3;
79 use base 'DBIx::Class::Core';
80 __PACKAGE__->load_components(qw(UTF8Columns +A::SubComp));
81 sub store_column { shift->next::method (@_) };
82 1;
83 },
84 [qr/Incorrect loading order of DBIx::Class::UTF8Columns.+affect other components overriding 'store_column' \(A::SubComp \(via A::Comp\)\)/],
85 'incorrect order warning issued (violator inherits)',
86);
72ae8e40 87
a47e1233 88my $schema = DBICTest->init_schema();
404939a4 89DBICTest::Schema::CD->load_components('UTF8Columns');
90DBICTest::Schema::CD->utf8_columns('title');
70350518 91Class::C3->reinitialize();
92
fed2c7d7 93# as per http://search.cpan.org/dist/Test-Simple/lib/Test/More.pm#utf8
94binmode (Test::More->builder->$_, ':utf8') for qw/output failure_output todo_output/;
cdd254a5 95
96my $bytestream_title = my $utf8_title = "weird \x{466} stuff";
97utf8::encode($bytestream_title);
98cmp_ok ($bytestream_title, 'ne', $utf8_title, 'unicode/raw differ (sanity check)');
99
100my $storage = $schema->storage;
5cfb131d 101my ($sql, @bind);
102my $debugobj = DBIC::DebugObj->new (\$sql, \@bind);
103my ($orig_debug, $orig_debugobj) = ($storage->debug, $storage->debugobj);
104$storage->debugobj ($debugobj);
cdd254a5 105$storage->debug (1);
337c98ef 106
cdd254a5 107my $cd = $schema->resultset('CD')->create( { artist => 1, title => $utf8_title, year => '2048' } );
1d0057bd 108
5cfb131d 109$storage->debugobj ($orig_debugobj);
110$storage->debug ($orig_debug);
111
112# bind values are always alphabetically ordered by column, thus [1]
113# the single quotes are an artefact of the debug-system
4ca1fd6f 114{
cdd254a5 115 local $TODO = "This has been broken since rev 1191, Mar 2006";
5cfb131d 116 is ($bind[1], "'$bytestream_title'", 'INSERT: raw bytes sent to the database');
cdd254a5 117}
118
119# this should be using the cursor directly, no inflation/processing of any sort
120my ($raw_db_title) = $schema->resultset('CD')
121 ->search ($cd->ident_condition)
122 ->get_column('title')
123 ->_resultset
124 ->cursor
125 ->next;
126
127is ($raw_db_title, $bytestream_title, 'INSERT: raw bytes retrieved from database');
128
129for my $reloaded (0, 1) {
130 my $test = $reloaded ? 'reloaded' : 'stored';
131 $cd->discard_changes if $reloaded;
1d0057bd 132
cdd254a5 133 ok( utf8::is_utf8( $cd->title ), "got $test title with utf8 flag" );
134 ok(! utf8::is_utf8( $cd->{_column_data}{title} ), "in-object $test title without utf8" );
135
136 ok(! utf8::is_utf8( $cd->year ), "got $test year without utf8 flag" );
137 ok(! utf8::is_utf8( $cd->{_column_data}{year} ), "in-object $test year without utf8" );
138}
139
140$cd->title('nonunicode');
141ok(! utf8::is_utf8( $cd->title ), 'update title without utf8 flag' );
142ok(! utf8::is_utf8( $cd->{_column_data}{title} ), 'store utf8-less title' );
143
144$cd->update;
145$cd->discard_changes;
146ok(! utf8::is_utf8( $cd->title ), 'reloaded title without utf8 flag' );
147ok(! utf8::is_utf8( $cd->{_column_data}{title} ), 'reloaded utf8-less title' );
148
149$bytestream_title = $utf8_title = "something \x{219} else";
150utf8::encode($bytestream_title);
151
5cfb131d 152
153$storage->debugobj ($debugobj);
154$storage->debug (1);
155
cdd254a5 156$cd->update ({ title => $utf8_title });
5cfb131d 157
158$storage->debugobj ($orig_debugobj);
159$storage->debug ($orig_debug);
160
161is ($bind[0], "'$bytestream_title'", 'UPDATE: raw bytes sent to the database');
cdd254a5 162($raw_db_title) = $schema->resultset('CD')
163 ->search ($cd->ident_condition)
164 ->get_column('title')
165 ->_resultset
166 ->cursor
167 ->next;
168is ($raw_db_title, $bytestream_title, 'UPDATE: raw bytes retrieved from database');
169
170$cd->discard_changes;
171$cd->title($utf8_title);
1d0057bd 172ok( !$cd->is_column_changed('title'), 'column is not dirty after setting the same unicode value' );
173
cdd254a5 174$cd->update ({ title => $utf8_title });
1d0057bd 175$cd->title('something_else');
176ok( $cd->is_column_changed('title'), 'column is dirty after setting to something completely different');
db087eb1 177
4ca1fd6f 178{
db087eb1 179 local $TODO = 'There is currently no way to propagate aliases to inflate_result()';
cdd254a5 180 $cd = $schema->resultset('CD')->find ({ title => $utf8_title }, { select => 'title', as => 'name' });
55087b99 181 ok (utf8::is_utf8( $cd->get_column ('name') ), 'utf8 flag propagates via as');
db087eb1 182}
183
55087b99 184done_testing;