1+ <?php
2+
3+ namespace EventListener \DcaField ;
4+
5+ use Contao \CoreBundle \Framework \ContaoFramework ;
6+ use Contao \CoreBundle \Slug \Slug ;
7+ use Contao \Database ;
8+ use Contao \DataContainer ;
9+ use Dflydev \DotAccessData \Data ;
10+ use HeimrichHannot \UtilsBundle \Dca \AliasField ;
11+ use HeimrichHannot \UtilsBundle \EventListener \DcaField \AliasDcaFieldListener ;
12+ use HeimrichHannot \UtilsBundle \Tests \AbstractUtilsTestCase ;
13+ use PHPUnit \Framework \MockObject \MockBuilder ;
14+ use Psr \Container \ContainerInterface ;
15+ use function Clue \StreamFilter \fun ;
16+
17+ class AliasDcaFieldListenerTest extends AbstractUtilsTestCase
18+ {
19+
20+ public function getTestInstance (array $ parameters = [], ?MockBuilder $ mockBuilder = null )
21+ {
22+ $ container = $ parameters ['container ' ] ?? $ this ->createMock (ContainerInterface::class);
23+
24+ return new AliasDcaFieldListener ($ container );
25+ }
26+
27+ public function testOnLoadDataContainer ()
28+ {
29+ $ GLOBALS ['TL_DCA ' ]['tl_test ' ] = [];
30+
31+ $ instance = $ this ->getTestInstance ();
32+ $ instance ->onLoadDataContainer ('tl_test ' );
33+ $ this ->assertEmpty ($ GLOBALS ['TL_DCA ' ]['tl_test ' ]);
34+
35+ AliasField::register ('tl_test ' );
36+ $ instance ->onLoadDataContainer ('tl_test ' );
37+ $ this ->assertArrayHasKey ('fields ' , $ GLOBALS ['TL_DCA ' ]['tl_test ' ]);
38+ $ this ->assertArrayHasKey ('alias ' , $ GLOBALS ['TL_DCA ' ]['tl_test ' ]['fields ' ]);
39+ $ this ->assertSame (
40+ [AliasDcaFieldListener::class, 'onFieldsAliasSaveCallback ' ],
41+ $ GLOBALS ['TL_DCA ' ]['tl_test ' ]['fields ' ]['alias ' ]['save_callback ' ][0 ]
42+ );
43+
44+ AliasField::register ('tl_test ' )->setAliasExistCallback (null );
45+ $ instance ->onLoadDataContainer ('tl_test ' );
46+ $ this ->assertArrayHasKey ('fields ' , $ GLOBALS ['TL_DCA ' ]['tl_test ' ]);
47+ $ this ->assertArrayHasKey ('alias ' , $ GLOBALS ['TL_DCA ' ]['tl_test ' ]['fields ' ]);
48+ $ this ->assertEmpty (
49+ $ GLOBALS ['TL_DCA ' ]['tl_test ' ]['fields ' ]['alias ' ]['save_callback ' ]
50+ );
51+ }
52+
53+ public function testOnFieldsAliasSaveCallbackGeneratesAliasIfEmpty ()
54+ {
55+ $ slug = $ this ->createMock (Slug::class);
56+ $ slug ->expects ($ this ->once ())
57+ ->method ('generate ' )
58+ ->willReturn ('generated-alias ' );
59+
60+ $ framework = $ this ->createMock (ContaoFramework::class);
61+
62+ $ container = $ this ->createMock (ContainerInterface::class);
63+ $ container ->method ('get ' )->willReturnCallback (function (string $ id ) use ($ slug , $ framework ) {
64+ switch ($ id ) {
65+ case 'contao.slug ' :
66+ case Slug::class:
67+ return $ slug ;
68+ case 'contao.framework ' :
69+ return $ framework ;
70+ default :
71+ throw new \InvalidArgumentException ("Unknown service: $ id " );
72+ }
73+ });
74+
75+ $ listener = $ this ->getTestInstance ([
76+ 'container ' => $ container ,
77+ ]);
78+
79+ $ dc = new class () extends DataContainer
80+ {
81+ public int $ id ;
82+ public string $ table ;
83+ public object $ activeRecord ;
84+
85+ public function __construct ()
86+ {
87+ }
88+
89+ public function __get ($ strKey )
90+ {
91+ if (isset ($ this ->{$ strKey })) {
92+ return $ this ->{$ strKey };
93+ }
94+
95+ return parent ::__get ($ strKey );
96+ }
97+
98+ public function __set ($ strKey , $ varValue )
99+ {
100+ if (isset ($ this ->{$ strKey })) {
101+ $ this ->{$ strKey } = $ varValue ;
102+ } else {
103+ parent ::__set ($ strKey , $ varValue );
104+ }
105+ }
106+
107+ public function getPalette ()
108+ {
109+ // TODO: Implement getPalette() method.
110+ }
111+
112+ protected function save ($ varValue )
113+ {
114+ // TODO: Implement save() method.
115+ }
116+ };
117+
118+ // $dc = $this->createMock(DataContainer::class);
119+ $ dc ->activeRecord = (object )['title ' => 'Test ' , 'pid ' => 1 ];
120+ $ dc ->table = 'tl_article ' ;
121+ $ dc ->id = 1 ;
122+
123+ $ result = $ listener ->onFieldsAliasSaveCallback ('' , $ dc );
124+ $ this ->assertEquals ('generated-alias ' , $ result );
125+ }
126+
127+ public function testOnFieldsAliasSaveCallbackThrowsOnNumericAlias ()
128+ {
129+ $ this ->expectException (\Exception::class);
130+
131+ $ slug = $ this ->createMock (Slug::class);
132+ $ framework = $ this ->mockContaoFramework ();
133+ $ framework ->method ('createInstance ' )->willReturn ($ this ->createMock (Database::class));
134+
135+ $ container = $ this ->createMock (ContainerInterface::class);
136+ $ container ->method ('get ' )->willReturnCallback (function (string $ id ) use ($ slug , $ framework ) {
137+ switch ($ id ) {
138+ case 'contao.slug ' :
139+ case Slug::class:
140+ return $ slug ;
141+ case 'contao.framework ' :
142+ return $ framework ;
143+ default :
144+ throw new \InvalidArgumentException ("Unknown service: $ id " );
145+ }
146+ });
147+
148+
149+
150+ $ listener = $ this ->getTestInstance ([
151+ 'container ' => $ container ,
152+ ]);
153+
154+ $ dc = $ this ->createMock (DataContainer::class);
155+ $ dc ->activeRecord = (object )['title ' => 'Test ' , 'pid ' => 1 ];
156+ $ dc ->table = 'tl_article ' ;
157+ $ dc ->id = 1 ;
158+
159+ $ GLOBALS ['TL_LANG ' ]['ERR ' ]['aliasNumeric ' ] = 'Alias darf nicht numerisch sein: %s ' ;
160+
161+ $ listener ->onFieldsAliasSaveCallback ('123 ' , $ dc );
162+ }
163+
164+ public function testOnFieldsAliasSaveCallbackThrowsOnExistingAlias ()
165+ {
166+ $ this ->expectException (\Exception::class);
167+
168+ $ slug = $ this ->createMock (Slug::class);
169+
170+ $ dbResult = new \stdClass ();
171+ $ dbResult ->numRows = 1 ;
172+
173+ $ db = $ this ->getMockBuilder (Database::class)
174+ ->disableOriginalConstructor ()
175+ ->onlyMethods (['prepare ' , 'execute ' ])
176+ ->getMock ();
177+ $ db ->method ('prepare ' )->willReturnSelf ();
178+ $ db ->method ('execute ' )->willReturn ($ dbResult );
179+
180+ $ framework = $ this ->mockContaoFramework ();
181+ $ framework ->method ('createInstance ' )->willReturn ($ db );
182+
183+ $ container = $ this ->createMock (ContainerInterface::class);
184+ $ container ->method ('get ' )->willReturnCallback (function (string $ id ) use ($ slug , $ framework ) {
185+ switch ($ id ) {
186+ case 'contao.slug ' :
187+ case Slug::class:
188+ return $ slug ;
189+ case 'contao.framework ' :
190+ return $ framework ;
191+ default :
192+ throw new \InvalidArgumentException ("Unknown service: $ id " );
193+ }
194+ });
195+
196+ $ listener = $ this ->getTestInstance ([
197+ 'container ' => $ container ,
198+ ]);
199+
200+ $ dc = $ this ->createMock (DataContainer::class);
201+ $ dc ->activeRecord = (object )['title ' => 'Test ' , 'pid ' => 1 ];
202+ $ dc ->table = 'tl_article ' ;
203+ $ dc ->id = 1 ;
204+
205+ $ GLOBALS ['TL_LANG ' ]['ERR ' ]['aliasExists ' ] = 'Alias existiert bereits: %s ' ;
206+
207+ $ listener ->onFieldsAliasSaveCallback ('existing-alias ' , $ dc );
208+ }
209+
210+ }
0 commit comments