Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fgetcsvのテストを追加 #223

Merged
merged 5 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions data/class/SC_Initial.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,17 @@ public function phpconfigInit()
//ロケールを明示的に設定
$res = setlocale(LC_ALL, LOCALE);
if ($res === FALSE) {
// TODO: Windows上のロケール設定が正常に働かない場合があることに暫定的に対応
// ''を指定するとApache実行環境の環境変数が使われる
// See also: http://php.net/manual/ja/function.setlocale.php
setlocale(LC_ALL, '');
if ('\\' === DIRECTORY_SEPARATOR && PHP_VERSION_ID >= 70000) {
// Windows 版 PHP7 以降の fgetcsv 関数は、日本語のロケールで
// UTF-8 のファイルを正常にパースできないため、ロケールを英語に設定する
// see https://github.com/EC-CUBE/ec-cube/issues/1780#issuecomment-248557386
setlocale(LC_ALL, 'English_United States.1252');
} else {
// TODO: Windows上のロケール設定が正常に働かない場合があることに暫定的に対応
// ''を指定するとApache実行環境の環境変数が使われる
// See also: http://php.net/manual/ja/function.setlocale.php
setlocale(LC_ALL, '');
}
}

// #1849 (文字エンコーディングの検出を制御する)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

$HOME = realpath(dirname(__FILE__)) . "/../../../../..";
require_once($HOME . "/tests/class/Common_TestCase.php");
/**
*
*/
class LC_Page_Admin_Products_UploadCSVTest extends Common_TestCase
{
protected function setUp()
{
parent::setUp();
if ('\\' !== DIRECTORY_SEPARATOR) {
$this->markTestSkipped('Windows only');
}
}

protected function tearDown()
{
parent::tearDown();
// ロケールを初期化する
$initial = new SC_Initial();
$initial->phpconfigInit();
}

public function test日本語ロケールで商品csvアップロード()
{
// 日本語版 Windows のロケールを設定する
setLocale(LC_ALL, 'Japanese_Japan.932');

$HOME = realpath(dirname(__FILE__)) . "/../../../../..";
$filepath = $HOME . "/tests/product.csv";
$enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, CHAR_CODE, CSV_TEMP_REALDIR);

$fp = fopen($enc_filepath, 'r');

$line_count = 0;
while (!feof($fp)) {
$arrCSV = fgetcsv($fp, CSV_LINE_MAX);
// 空行はスキップ
if (empty($arrCSV)) {
continue;
}
// 行カウント
$line_count++;

$this->expected = 71;
$this->assertNotCount($this->expected, $arrCSV, '日本語のロケールでは '.$line_count.' 列目の行数が不正になるはず');

// ヘッダ行はスキップ
if ($line_count == 1) {
continue;
}
}
fclose($fp);

$this->expected = 10;
$this->actual = $line_count;

$this->verify('列数');
}

public function testSC_Initialのロケールで商品csvアップロード()
{
$HOME = realpath(dirname(__FILE__)) . "/../../../../..";
$filepath = $HOME . "/tests/product.csv";
$enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, CHAR_CODE, CSV_TEMP_REALDIR);

$fp = fopen($enc_filepath, 'r');

$line_count = 0;
while (!feof($fp)) {
$arrCSV = fgetcsv($fp, CSV_LINE_MAX);
// 空行はスキップ
if (empty($arrCSV)) {
continue;
}
// 行カウント
$line_count++;

$this->expected = 71;
$this->assertCount($this->expected, $arrCSV, $line_count.' 列目の行数は一致する');

// ヘッダ行はスキップ
if ($line_count == 1) {
continue;
}
}
fclose($fp);

$this->expected = 10;
$this->actual = $line_count;

$this->verify('列数');
}

public function test英語のロケールで商品csvアップロード()
{
// 英語版 Windows のロケールを設定する
setlocale(LC_ALL, 'English_United States.1252');

$HOME = realpath(dirname(__FILE__)) . "/../../../../..";
$filepath = $HOME . "/tests/product.csv";
$enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, CHAR_CODE, CSV_TEMP_REALDIR);

$fp = fopen($enc_filepath, 'r');

$line_count = 0;
while (!feof($fp)) {
$arrCSV = fgetcsv($fp, CSV_LINE_MAX);
// 空行はスキップ
if (empty($arrCSV)) {
continue;
}
// 行カウント
$line_count++;

$this->expected = 71;
$this->assertCount($this->expected, $arrCSV, $line_count.' 列目の行数は一致する');

// ヘッダ行はスキップ
if ($line_count == 1) {
continue;
}
}
fclose($fp);

$this->expected = 10;
$this->actual = $line_count;

$this->verify('列数');
}
}
10 changes: 10 additions & 0 deletions tests/product.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
���iID,���i�K�iID,�e�K�i����ID,�K�i����ID,�e�K�i���ޖ�,�K�i���ޖ�,���[�J�[ID,���[�J�[��,���i��,"�\���X�e�[�^�X(���J�E����J)",���[�J�[URL(�R�����g1),�R�����g2,�������[�h(�R�����g3),�R�����g4,�R�����g5,�R�����g6,���l��(SHOP��p),�ꗗ-���C���R�����g,�ꗗ-���C���摜,�ڍ�-���C���R�����g,�ڍ�-���C���摜,�ڍ�-���C���g��摜,�ڍ�-�T�u�^�C�g��(1),�ڍ�-�T�u�R�����g(1),�ڍ�-�T�u�摜(1),�ڍ�-�T�u�g��摜(1),�ڍ�-�T�u�^�C�g��(2),�ڍ�-�T�u�R�����g(2),�ڍ�-�T�u�摜(2),�ڍ�-�T�u�g��摜(2),�ڍ�-�T�u�^�C�g��(3),�ڍ�-�T�u�R�����g(3),�ڍ�-�T�u�摜(3),�ڍ�-�T�u�g��摜(3),�ڍ�-�T�u�^�C�g��(4),�ڍ�-�T�u�R�����g(4),�ڍ�-�T�u�摜(4),�ڍ�-�T�u�g��摜(4),�ڍ�-�T�u�^�C�g��(5),�ڍ�-�T�u�R�����g(5),�ڍ�-�T�u�摜(5),�ڍ�-�T�u�g��摜(5),�������ڈ�ID,�폜�t���O,���i���ID,���i�R�[�h,�݌ɐ�,�݌ɖ������t���O,�̔�������,�ʏ퉿�i,�̔����i,����,�|�C���g�t�^��,�_�E�����[�h�t�@�C����,�_�E�����[�h���t�@�C��,�֘A���iID(1),�֘A���i�R�����g(1),�֘A���iID(2),�֘A���i�R�����g(2),�֘A���iID(3),�֘A���i�R�����g(3),�֘A���iID(4),�֘A���i�R�����g(4),�֘A���iID(5),�֘A���i�R�����g(5),�֘A���iID(6),�֘A���i�R�����g(6),���i�X�e�[�^�X,���i�X�e�[�^�X��,�J�e�S��ID,�J�e�S����
1,1,6,3,S,����,,,�A�C�X�N���[��,1,,,"�A�C�X,�o�j��,�`���R,����",,,,,�����Ăɂǂ����B,ice130.jpg,�₽�����̂͂������ł���?,ice260.jpg,ice500.jpg,,<b>����������<b>,,,,,,,,,,,,,,,,,,,2,0,1,ice-01,,1,,1000,933,,10,,,,,,,,,,,,,,,1,NEW,5,�A�C�X
1,4,6,2,S,�`���R,,,�A�C�X�N���[��,1,,,"�A�C�X,�o�j��,�`���R,����",,,,,�����Ăɂǂ����B,ice130.jpg,�₽�����̂͂������ł���?,ice260.jpg,ice500.jpg,,<b>����������<b>,,,,,,,,,,,,,,,,,,,2,0,1,ice-04,,1,,1000,933,,10,,,,,,,,,,,,,,,1,NEW,5,�A�C�X
1,5,5,2,M,�`���R,,,�A�C�X�N���[��,1,,,"�A�C�X,�o�j��,�`���R,����",,,,,�����Ăɂǂ����B,ice130.jpg,�₽�����̂͂������ł���?,ice260.jpg,ice500.jpg,,<b>����������<b>,,,,,,,,,,,,,,,,,,,2,0,1,ice-05,,1,,1000,933,,10,,,,,,,,,,,,,,,1,NEW,5,�A�C�X
1,6,4,2,L,�`���R,,,�A�C�X�N���[��,1,,,"�A�C�X,�o�j��,�`���R,����",,,,,�����Ăɂǂ����B,ice130.jpg,�₽�����̂͂������ł���?,ice260.jpg,ice500.jpg,,<b>����������<b>,,,,,,,,,,,,,,,,,,,2,0,1,ice-06,,1,,1000,933,,10,,,,,,,,,,,,,,,1,NEW,5,�A�C�X
1,7,6,1,S,�o�j��,,,�A�C�X�N���[��,1,,,"�A�C�X,�o�j��,�`���R,����",,,,,�����Ăɂǂ����B,ice130.jpg,�₽�����̂͂������ł���?,ice260.jpg,ice500.jpg,,<b>����������<b>,,,,,,,,,,,,,,,,,,,2,0,1,ice-07,,1,,1000,933,,10,,,,,,,,,,,,,,,1,NEW,5,�A�C�X
1,8,5,1,M,�o�j��,,,�A�C�X�N���[��,1,,,"�A�C�X,�o�j��,�`���R,����",,,,,�����Ăɂǂ����B,ice130.jpg,�₽�����̂͂������ł���?,ice260.jpg,ice500.jpg,,<b>����������<b>,,,,,,,,,,,,,,,,,,,2,0,1,ice-08,,1,,1000,933,,10,,,,,,,,,,,,,,,1,NEW,5,�A�C�X
1,9,4,1,L,�o�j��,,,�A�C�X�N���[��,1,,,"�A�C�X,�o�j��,�`���R,����",,,,,�����Ăɂǂ����B,ice130.jpg,�₽�����̂͂������ł���?,ice260.jpg,ice500.jpg,,<b>����������<b>,,,,,,,,,,,,,,,,,,,2,0,1,ice-09,,1,,1000,933,,10,,,,,,,,,,,,,,,1,NEW,5,�A�C�X
2,10,0,0,,,,,���Ȃ�,1,,,"��,�Ȃ�,�i�x",,,,,��l�p���炠��܂��B,nabe130.jpg,���܂ɂ͓�ł��ǂ��ł��傤�B,nabe260.jpg,nabe500.jpg,,,,,,,,,,,,,,,,,,,,,3,0,1,nabe-01,99,0,5,1700,1650,,10,,,1,���������ɁB,,,,,,,,,,,,,4,�Ȃ�
3,11,0,0,,,,,���Ȃ׃��V�s,1,,,"��,�Ȃ�,�i�x,���V�s,����",,,,,���́A��`�̂��烌�V�s�ł��B,recipe130.jpg,"<b>���̏��i�̓_�E�����[�h���i�ł�</b><br />�����Ń`�������W���Ă݂������ɁB",recipe260.jpg,recipe500.jpg,,,,,,,,,,,,,,,,,,,,,1,0,2,recipe-01,,1,,,100,,10,���Ȃ׃��V�s.pdf,recipe_onabe.pdf,,,,,,,,,,,,,"4,5","�I�X�X��,����i","4,6","�Ȃ�,���V�s"