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