33 * Copyright 2011 Adobe
44 * All Rights Reserved.
55 */
6+ declare (strict_types=1 );
67
78namespace Magento \Customer \Model ;
89
9- class GroupTest extends \PHPUnit \Framework \TestCase
10+ use Magento \Customer \Api \Data \GroupInterfaceFactory ;
11+ use Magento \Framework \Exception \LocalizedException ;
12+ use Magento \TestFramework \Helper \Bootstrap ;
13+ use PHPUnit \Framework \TestCase ;
14+
15+ class GroupTest extends TestCase
1016{
1117 /**
12- * @var \Magento\Customer\Model\ Group
18+ * @var Group
1319 */
1420 protected $ groupModel ;
1521
1622 /**
17- * @var \Magento\Customer\Api\Data\ GroupInterfaceFactory
23+ * @var GroupInterfaceFactory
1824 */
1925 protected $ groupFactory ;
2026
2127 protected function setUp (): void
2228 {
23- $ this ->groupModel = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
24- \Magento \Customer \Model \Group::class
25- );
26- $ this ->groupFactory = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
27- \Magento \Customer \Api \Data \GroupInterfaceFactory::class
28- );
29+ $ this ->groupModel = Bootstrap::getObjectManager ()->create (Group::class);
30+ $ this ->groupFactory = Bootstrap::getObjectManager ()->create (GroupInterfaceFactory::class);
2931 }
3032
3133 public function testCRUD ()
@@ -36,43 +38,74 @@ public function testCRUD()
3638 }
3739
3840 /**
39- * Test that customer group correctly handles multibyte characters when saving
41+ * Test that customer group correctly handles multibyte and normal characters when saving
4042 *
4143 * This verifies that the fix for multibyte character truncation works correctly.
4244 * Previously, substr() was used which counted bytes instead of characters,
4345 * causing multibyte characters to be truncated incorrectly.
4446 *
4547 * @magentoDbIsolation enabled
48+ * @dataProvider customerGroupCodeDataProvider
49+ * @param string $code
50+ * @param string $expectedCode
51+ * @param int $charLength
4652 * @return void
53+ * @throws LocalizedException
4754 */
48- public function testMultibyteCharacterHandling ( ): void
55+ public function testMultibyteAndNormalCharacterHandling ( string $ code , string $ expectedCode , int $ charLength ): void
4956 {
50- // Test with multibyte characters (ö = 2 bytes in UTF-8)
51- $ multibyteString = str_repeat ('ö ' , 31 ); // 31 characters, 62 bytes
52-
53- $ group = $ this ->groupFactory ->create ();
54- $ group ->setCode ($ multibyteString );
55- $ group ->setTaxClassId (3 );
56- $ group ->save ();
57+ $ this ->groupModel ->setCode ($ code );
58+ $ this ->groupModel ->setTaxClassId (3 );
59+ $ group = $ this ->groupModel ->save ();
5760
5861 // Reload from database
59- $ reloadedGroup = $ this ->groupFactory ->create ();
60- $ reloadedGroup ->load ($ group ->getId ());
62+ $ reloadedGroup = $ this ->groupModel ->load ($ group ->getId ());
6163
62- // Verify all 31 multibyte characters are preserved
64+ // Verify all 32 characters are preserved
6365 $ this ->assertEquals (
64- $ multibyteString ,
66+ $ expectedCode ,
6567 $ reloadedGroup ->getCode (),
66- 'Group code with multibyte characters should be saved correctly '
68+ 'Group code with multibyte and normal characters should be saved correctly '
6769 );
6870
6971 $ this ->assertEquals (
70- 31 ,
72+ $ charLength ,
7173 mb_strlen ($ reloadedGroup ->getCode ()),
72- 'Group code should have exactly 31 characters '
74+ 'Group code should have maximum 32 characters '
7375 );
7476
7577 // Cleanup
7678 $ reloadedGroup ->delete ();
7779 }
80+
81+ /**
82+ * Customer group code data provider
83+ *
84+ * @return array[]
85+ */
86+ public static function customerGroupCodeDataProvider (): array
87+ {
88+ // Test with multibyte characters (ö = 2 bytes in UTF-8)
89+ $ multibyteString = str_repeat ('ö ' , 32 ); // 31 characters, 62 bytes
90+ $ normalString = str_repeat ('a ' , 50 ); // 40 characters, will be truncated
91+ $ normalTruncatedString = str_repeat ('a ' , 32 ); // 31 characters, truncated code after saving
92+ $ mixedString = str_repeat ('a ' , 10 ).str_repeat ('ö ' , 10 );
93+ return [
94+ 'multibyte characters ' => [
95+ $ multibyteString ,
96+ $ multibyteString ,
97+ 32
98+ ],
99+ 'normal characters ' => [
100+ $ normalString ,
101+ $ normalTruncatedString ,
102+ 32
103+ ],
104+ 'mixed characters ' => [
105+ $ mixedString ,
106+ $ mixedString ,
107+ 20
108+ ]
109+ ];
110+ }
78111}
0 commit comments