1212use DateTime ;
1313use OCA \ContextChat \AppInfo \Application ;
1414use OCA \ContextChat \BackgroundJobs \InitialContentImportJob ;
15- use OCA \ContextChat \BackgroundJobs \ SubmitContentJob ;
15+ use OCA \ContextChat \Db \ QueueContentItem ;
1616use OCA \ContextChat \Db \QueueContentItemMapper ;
1717use OCA \ContextChat \Event \ContentProviderRegisterEvent ;
1818use OCA \ContextChat \Logger ;
2121use OCA \ContextChat \Public \IContentProvider ;
2222use OCA \ContextChat \Service \ActionScheduler ;
2323use OCA \ContextChat \Service \ProviderConfigService ;
24+ use OCP \AppFramework \Services \IAppConfig ;
2425use OCP \BackgroundJob \IJobList ;
2526use OCP \EventDispatcher \IEventDispatcher ;
2627use OCP \IServerContainer ;
3132use Test \TestCase ;
3233
3334class ContentManagerTest extends TestCase {
35+ /** @var MockObject | IAppConfig */
36+ private IAppConfig $ appConfig ;
3437 /** @var MockObject | QueueContentItemMapper */
3538 private QueueContentItemMapper $ mapper ;
3639 /** @var MockObject | ProviderConfigService */
@@ -52,6 +55,7 @@ public function setUp(): void {
5255 $ this ->jobList = Server::get (IJobList::class);
5356 $ this ->logger = Server::get (LoggerInterface::class);
5457
58+ $ this ->appConfig = $ this ->createMock (IAppConfig::class);
5559 $ this ->mapper = $ this ->createMock (QueueContentItemMapper::class);
5660 $ this ->providerConfig = $ this ->createMock (ProviderConfigService::class);
5761 $ this ->actionService = $ this ->createMock (ActionScheduler::class);
@@ -92,6 +96,7 @@ public function setUp(): void {
9296
9397 $ this ->contentManager = new ContentManager (
9498 $ this ->jobList ,
99+ $ this ->appConfig ,
95100 $ this ->providerConfig ,
96101 $ this ->mapper ,
97102 $ this ->actionService ,
@@ -167,17 +172,64 @@ public function testSubmitContent(): void {
167172 ),
168173 ];
169174
175+ $ this ->mapper
176+ ->expects ($ this ->once ())
177+ ->method ('findIdByUniqueKey ' )
178+ ->with ($ appId , 'provider-id ' , 'item-id ' )
179+ ->willReturn (null );
180+
170181 $ this ->mapper
171182 ->expects ($ this ->once ())
172183 ->method ('insert ' );
173184
174- $ this ->jobList ->remove (SubmitContentJob::class, null );
175- $ this ->assertFalse ($ this ->jobList ->has (SubmitContentJob::class, null ));
185+ $ this ->mapper
186+ ->expects ($ this ->never ())
187+ ->method ('update ' );
176188
177189 $ this ->contentManager ->submitContent ($ appId , $ items );
190+ }
191+
192+ public function testSubmitContentUpdatesAlreadyQueuedItem (): void {
193+ $ appId = 'test ' ;
194+ $ items = [
195+ new ContentItem (
196+ 'item-id ' ,
197+ 'provider-id ' ,
198+ 'new title ' ,
199+ 'new content ' ,
200+ 'email-file ' ,
201+ new DateTime (),
202+ ['user1 ' , 'user2 ' ],
203+ ),
204+ ];
178205
179- $ this ->assertTrue ($ this ->jobList ->has (SubmitContentJob::class, null ));
180- $ this ->jobList ->remove (SubmitContentJob::class, null );
206+ $ this ->mapper
207+ ->expects ($ this ->once ())
208+ ->method ('findIdByUniqueKey ' )
209+ ->with ($ appId , 'provider-id ' , 'item-id ' )
210+ ->willReturn (42 );
211+
212+ $ this ->mapper
213+ ->expects ($ this ->never ())
214+ ->method ('insert ' );
215+
216+ $ this ->mapper
217+ ->expects ($ this ->once ())
218+ ->method ('update ' )
219+ ->with ($ this ->callback (function (QueueContentItem $ dbItem ) use ($ appId ) {
220+ // update() needs the id of the already queued row, otherwise
221+ // QBMapper throws "Entity which should be updated has no id"
222+ $ this ->assertSame (42 , $ dbItem ->getId ());
223+ $ this ->assertSame ($ appId , $ dbItem ->getAppId ());
224+ $ this ->assertSame ('provider-id ' , $ dbItem ->getProviderId ());
225+ $ this ->assertSame ('item-id ' , $ dbItem ->getItemId ());
226+ $ this ->assertSame ('new title ' , $ dbItem ->getTitle ());
227+ $ this ->assertSame ('new content ' , $ dbItem ->getContent ());
228+ $ this ->assertSame ('user1,user2 ' , $ dbItem ->getUsers ());
229+ return true ;
230+ }));
231+
232+ $ this ->contentManager ->submitContent ($ appId , $ items );
181233 }
182234}
183235
0 commit comments