1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2014 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
@@ -385,18 +385,138 @@ public function testExecute(
385
385
}
386
386
}
387
387
388
+ /**
389
+ * Test that canSendNewShipmentEmail is called with correct store ID
390
+ *
391
+ * @dataProvider storeIdDataProvider
392
+ */
393
+ public function testCanSendNewShipmentEmailWithStoreId (
394
+ int $ storeId ,
395
+ bool $ sendEmailRequested ,
396
+ bool $ emailEnabledForStore ,
397
+ bool $ shouldSendEmail
398
+ ): void {
399
+ $ this ->formKeyValidator ->expects ($ this ->once ())
400
+ ->method ('validate ' )
401
+ ->willReturn (true );
402
+
403
+ $ this ->request ->expects ($ this ->once ())
404
+ ->method ('isPost ' )
405
+ ->willReturn (true );
406
+
407
+ $ shipmentId = 1000012 ;
408
+ $ orderId = 10003 ;
409
+ $ shipmentData = ['items ' => [], 'send_email ' => $ sendEmailRequested ? 'on ' : '' ];
410
+
411
+ $ this ->request ->expects ($ this ->any ())
412
+ ->method ('getParam ' )
413
+ ->willReturnMap ([
414
+ ['order_id ' , null , $ orderId ],
415
+ ['shipment_id ' , null , $ shipmentId ],
416
+ ['shipment ' , null , $ shipmentData ],
417
+ ['tracking ' , null , []]
418
+ ]);
419
+
420
+ $ order = $ this ->createPartialMock (Order::class, ['setCustomerNoteNotify ' , 'getStoreId ' , '__wakeup ' ]);
421
+ $ order ->expects ($ this ->any ())
422
+ ->method ('getStoreId ' )
423
+ ->willReturn ($ storeId );
424
+
425
+ $ shipment = $ this ->createPartialMock (
426
+ Shipment::class,
427
+ ['load ' , 'save ' , 'register ' , 'getOrder ' , 'getOrderId ' , '__wakeup ' ]
428
+ );
429
+ $ shipment ->expects ($ this ->any ())
430
+ ->method ('getOrder ' )
431
+ ->willReturn ($ order );
432
+ $ shipment ->expects ($ this ->any ())
433
+ ->method ('getOrderId ' )
434
+ ->willReturn ($ orderId );
435
+
436
+ if ($ sendEmailRequested ) {
437
+ $ this ->salesData ->expects ($ this ->once ())
438
+ ->method ('canSendNewShipmentEmail ' )
439
+ ->with ($ storeId )
440
+ ->willReturn ($ emailEnabledForStore );
441
+ } else {
442
+ $ this ->salesData ->expects ($ this ->never ())
443
+ ->method ('canSendNewShipmentEmail ' );
444
+ }
445
+
446
+ if ($ shouldSendEmail ) {
447
+ $ this ->shipmentSender ->expects ($ this ->once ())
448
+ ->method ('send ' )
449
+ ->with ($ shipment );
450
+ } else {
451
+ $ this ->shipmentSender ->expects ($ this ->never ())
452
+ ->method ('send ' );
453
+ }
454
+
455
+ $ this ->shipmentLoader ->expects ($ this ->once ())
456
+ ->method ('load ' )
457
+ ->willReturn ($ shipment );
458
+
459
+ $ this ->setupCommonMocks ($ shipment , $ order , $ orderId );
460
+
461
+ $ this ->saveAction ->execute ();
462
+ }
463
+
464
+ /**
465
+ * Test that email is not sent when disabled for specific store but enabled globally
466
+ */
467
+ public function testEmailNotSentWhenDisabledForSpecificStore (): void
468
+ {
469
+ $ storeId = 2 ;
470
+ $ this ->testCanSendNewShipmentEmailWithStoreId (
471
+ $ storeId ,
472
+ true ,
473
+ false ,
474
+ false
475
+ );
476
+ }
477
+
478
+ /**
479
+ * Test that email is sent when enabled for specific store even if disabled globally
480
+ */
481
+ public function testEmailSentWhenEnabledForSpecificStore (): void
482
+ {
483
+ $ storeId = 2 ;
484
+ $ this ->testCanSendNewShipmentEmailWithStoreId (
485
+ $ storeId ,
486
+ true ,
487
+ true ,
488
+ true
489
+ );
490
+ }
491
+
492
+ /**
493
+ * @return array
494
+ */
495
+ public static function storeIdDataProvider (): array
496
+ {
497
+ return [
498
+ 'default_store_email_requested_enabled ' => [1 , true , true , true ],
499
+ 'default_store_email_requested_disabled ' => [1 , true , false , false ],
500
+ 'custom_store_email_requested_enabled ' => [2 , true , true , true ],
501
+ 'custom_store_email_requested_disabled ' => [2 , true , false , false ],
502
+ 'custom_store_email_not_requested ' => [2 , false , true , false ],
503
+ 'multistore_environment_store_3 ' => [3 , true , true , true ],
504
+ 'multistore_environment_store_5_disabled ' => [5 , true , false , false ],
505
+ ];
506
+ }
507
+
388
508
/**
389
509
* @return array
390
510
*/
391
511
public static function executeDataProvider (): array
392
512
{
393
513
/**
394
- * bool $formKeyIsValid
395
- * bool $isPost
396
- * string $sendEmail
397
- * bool $emailEnabled
398
- * bool $shouldEmailBeSent
399
- */
514
+ * bool $formKeyIsValid
515
+ * bool $isPost
516
+ * string $sendEmail
517
+ * bool $emailEnabled
518
+ * bool $shouldEmailBeSent
519
+ */
400
520
return [
401
521
[false , false , '' , false , false ],
402
522
[true , false , '' , false , false ],
@@ -409,6 +529,52 @@ public static function executeDataProvider(): array
409
529
];
410
530
}
411
531
532
+ /**
533
+ * Setup common mocks needed for successful execution
534
+ */
535
+ private function setupCommonMocks (MockObject $ shipment , MockObject $ order , int $ orderId ): void
536
+ {
537
+ $ shipment ->expects ($ this ->once ())
538
+ ->method ('register ' )
539
+ ->willReturnSelf ();
540
+
541
+ $ order ->expects ($ this ->once ())
542
+ ->method ('setCustomerNoteNotify ' );
543
+
544
+ $ this ->labelGenerator ->expects ($ this ->any ())
545
+ ->method ('create ' )
546
+ ->willReturn (true );
547
+
548
+ $ saveTransaction = $ this ->getMockBuilder (Transaction::class)
549
+ ->disableOriginalConstructor ()
550
+ ->getMock ();
551
+ $ saveTransaction ->method ('addObject ' )->willReturnSelf ();
552
+
553
+ $ this ->objectManager ->expects ($ this ->once ())
554
+ ->method ('create ' )
555
+ ->with (Transaction::class)
556
+ ->willReturn ($ saveTransaction );
557
+
558
+ $ this ->objectManager ->expects ($ this ->once ())
559
+ ->method ('get ' )
560
+ ->with (Session::class)
561
+ ->willReturn ($ this ->session );
562
+
563
+ $ this ->session ->expects ($ this ->once ())
564
+ ->method ('getCommentText ' )
565
+ ->with (true );
566
+
567
+ $ this ->shipmentValidatorMock ->expects ($ this ->once ())
568
+ ->method ('validate ' )
569
+ ->willReturn ($ this ->validationResult );
570
+
571
+ $ this ->validationResult ->expects ($ this ->once ())
572
+ ->method ('hasMessages ' )
573
+ ->willReturn (false );
574
+
575
+ $ this ->prepareRedirect (['order_id ' => $ orderId ]);
576
+ }
577
+
412
578
/**
413
579
* @param array $arguments
414
580
*
0 commit comments