"Fossies" - the Fresh Open Source Software Archive

Member "eprints3.4-3.4.4/tests/20_database_utf8.pl" (7 Aug 2022, 4088 Bytes) of package /linux/www/eprints3.4-3.4.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "20_database_utf8.pl": 3.4-3.4.3_vs_3.4-3.4.4.

    1 use strict;
    2 use Test::More tests => 10;
    3 
    4 BEGIN { use_ok( "EPrints" ); }
    5 BEGIN { use_ok( "EPrints::Test" ); }
    6 
    7 my $session = EPrints::Test::get_test_session( 0 );
    8 ok(defined $session, 'opened an EPrints::Session object');
    9 
   10 my $database = $session->get_database();
   11 ok( defined $database, "database defined" );
   12 
   13 my $dataset = $session->dataset( "eprint" );
   14 
   15 SKIP: {
   16     skip "Only supports MySQL", 3 unless $database->isa( "EPrints::Database::mysql" );
   17 
   18     my $sth;
   19 
   20     my $utf8_string = "XXX".chr(0xe9)."XXX".chr(0x169);
   21     my $table = "_utf8_test_".int(rand(1000));
   22 
   23     $database->do("DROP TABLE IF EXISTS $table");
   24 
   25     eval {
   26         # This checks MySQL behaves as we expect it to
   27 
   28         ### Legacy utf8-in-latin1
   29         # Create a latin1 table
   30         $database->do("SET NAMES 'latin1'");
   31         $database->do("CREATE TABLE $table (i CHAR(255) NOT NULL) CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci'");
   32         $database->do("INSERT INTO $table VALUES ('".$utf8_string."')");
   33 
   34         # We now have two bytes stored in a latin1 column
   35         $sth = $database->prepare("SELECT 1 FROM $table WHERE i like '\%e\%'");
   36         $sth->execute;
   37         # Which won't match using MySQL's collations
   38         ok(!defined $sth->fetch, "utf8-in-latin1");
   39 
   40         # We can now do the LATIN1->BINARY->UTF-8 trick
   41         $database->do("ALTER TABLE $table MODIFY i BINARY(255)");
   42         $database->do("ALTER TABLE $table MODIFY i CHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'");
   43 
   44         # We now have one character stored in utf8
   45         $sth = $database->prepare("SELECT 1 FROM $table WHERE i like '\%e\%'");
   46         $sth->execute;
   47         # Which will match 'e' using MySQL's collations
   48         ok(defined $sth->fetch, "utf-8 conversion");
   49 
   50         $database->do("DROP TABLE IF EXISTS $table");
   51 
   52         $database->do("SET NAMES 'utf8'");
   53         $database->do("CREATE TABLE $table (i CHAR(1) NOT NULL) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci'");
   54         $database->do("INSERT INTO $table VALUES ('".chr(0x169)."')");
   55         $sth = $database->prepare("SELECT * FROM $table");
   56         $sth->execute;
   57         my $c = Encode::decode_utf8($sth->fetch->[0]);
   58         ok($c eq chr(0x169), "CHAR(1) = 1 utf8 character");
   59         $database->do("DROP TABLE IF EXISTS $table");
   60     };
   61 
   62     $database->do("DROP TABLE IF EXISTS $table");
   63 }
   64 
   65 {
   66 my $utf8_byte = "\x{024b62}";
   67 # mysql only supports 1-3 byte UTF-8
   68 $utf8_byte = "\x{20ac}" if $database->isa( "EPrints::Database::mysql" );
   69 
   70 # watch-out: side-effecting database!
   71 my $eprintid = $database->counter_next( "eprintid" );
   72 
   73 my $field = $dataset->field( "eprint_status" );
   74 my $maxlength = $field->get_property( "maxlength" );
   75 my $testdata = $utf8_byte x $maxlength;
   76 my $table = $dataset->get_sql_table_name;
   77 
   78 ok($database->insert($table,[$dataset->key_field->get_sql_name,$field->get_sql_name],[$eprintid,$testdata]), "insert maximum size multi-byte characters");
   79 
   80 my $sth = $database->prepare("SELECT ".$database->quote_identifier($field->get_sql_name)." FROM ".$database->quote_identifier($table)." WHERE ".$database->quote_identifier($dataset->key_field->get_sql_name)."=$eprintid");
   81 $sth->execute;
   82 my( $stored ) = $sth->fetchrow_array;
   83 utf8::decode($stored) if !utf8::is_utf8($stored);
   84 
   85 is($stored,$testdata,"stored maximum size multi-byte characters");
   86 
   87 $database->delete_from($table,[$dataset->key_field->get_sql_name],[$eprintid]);
   88 }
   89 
   90 $session->terminate;
   91 
   92 ok(1);
   93 
   94 =head1 COPYRIGHT
   95 
   96 =for COPYRIGHT BEGIN
   97 
   98 Copyright 2022 University of Southampton.
   99 EPrints 3.4 is supplied by EPrints Services.
  100 
  101 http://www.eprints.org/eprints-3.4/
  102 
  103 =for COPYRIGHT END
  104 
  105 =for LICENSE BEGIN
  106 
  107 This file is part of EPrints 3.4 L<http://www.eprints.org/>.
  108 
  109 EPrints 3.4 and this file are released under the terms of the
  110 GNU Lesser General Public License version 3 as published by
  111 the Free Software Foundation unless otherwise stated.
  112 
  113 EPrints 3.4 is distributed in the hope that it will be useful,
  114 but WITHOUT ANY WARRANTY; without even the implied warranty of
  115 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  116 See the GNU Lesser General Public License for more details.
  117 
  118 You should have received a copy of the GNU Lesser General Public
  119 License along with EPrints 3.4.
  120 If not, see L<http://www.gnu.org/licenses/>.
  121 
  122 =for LICENSE END
  123