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