@@ -1739,7 +1739,7 @@ public function testSavePrimaryKeyEntityExists()
1739
1739
1740
1740
1741
1741
/**
1742
- * Test that save works with replace saveStrategy
1742
+ * Test that save works with replace saveStrategy and are not deleted once they are not null
1743
1743
*
1744
1744
* @return void
1745
1745
*/
@@ -1770,17 +1770,19 @@ public function testSaveReplaceSaveStrategy()
1770
1770
1771
1771
$ this ->assertEquals (2 , $ authors ->Articles ->find ('all ' )->where (['author_id ' => $ entity ['id ' ]])->count ());
1772
1772
1773
+ $ articleId = $ entity ->articles [0 ]->id ;
1773
1774
unset($ entity ->articles [0 ]);
1774
1775
$ entity ->dirty ('articles ' , true );
1775
1776
1776
1777
$ authors ->save ($ entity , ['associated ' => ['Articles ' ]]);
1777
1778
1778
1779
$ this ->assertEquals (1 , $ authors ->Articles ->find ('all ' )->where (['author_id ' => $ entity ['id ' ]])->count ());
1780
+ $ this ->assertTrue ($ authors ->Articles ->exists (['id ' => $ articleId ]));
1779
1781
}
1780
1782
1781
1783
1782
1784
/**
1783
- * Test that save works with append saveStrategy
1785
+ * Test that save works with append saveStrategy not deleting or setting null anything
1784
1786
*
1785
1787
* @return void
1786
1788
*/
@@ -1810,13 +1812,15 @@ public function testSaveAppendSaveStrategy()
1810
1812
$ entity = $ authors ->save ($ entity , ['associated ' => ['Articles ' ]]);
1811
1813
1812
1814
$ this ->assertEquals (2 , $ authors ->Articles ->find ('all ' )->where (['author_id ' => $ entity ['id ' ]])->count ());
1813
-
1815
+
1816
+ $ articleId = $ entity ->articles [0 ]->id ;
1814
1817
unset($ entity ->articles [0 ]);
1815
1818
$ entity ->dirty ('articles ' , true );
1816
1819
1817
1820
$ authors ->save ($ entity , ['associated ' => ['Articles ' ]]);
1818
1821
1819
1822
$ this ->assertEquals (2 , $ authors ->Articles ->find ('all ' )->where (['author_id ' => $ entity ['id ' ]])->count ());
1823
+ $ this ->assertTrue ($ authors ->Articles ->exists (['id ' => $ articleId ]));
1820
1824
}
1821
1825
/**
1822
1826
* Test that save has append as the default save strategy
@@ -1840,6 +1844,48 @@ public function testSaveDefaultSaveStrategy()
1840
1844
$ this ->assertEquals ('append ' , $ authors ->association ('articles ' )->saveStrategy ());
1841
1845
}
1842
1846
1847
+ /**
1848
+ * Test that the associated entities are unlinked and deleted when they are dependent
1849
+ *
1850
+ * @return void
1851
+ */
1852
+ public function testSaveReplaceSaveStrategyDependent ()
1853
+ {
1854
+ $ authors = $ this ->getMock (
1855
+ 'Cake\ORM\Table ' ,
1856
+ ['exists ' ],
1857
+ [
1858
+ [
1859
+ 'connection ' => $ this ->connection ,
1860
+ 'alias ' => 'Authors ' ,
1861
+ 'table ' => 'authors ' ,
1862
+ ]
1863
+ ]
1864
+ );
1865
+ $ authors ->hasMany ('Articles ' , ['saveStrategy ' => 'replace ' , 'dependent ' => true ]);
1866
+
1867
+ $ entity = $ authors ->newEntity ([
1868
+ 'name ' => 'mylux ' ,
1869
+ 'articles ' => [
1870
+ ['title ' => 'One Random Post ' , 'body ' => 'The cake is not a lie ' ],
1871
+ ['title ' => 'Another Random Post ' , 'body ' => 'The cake is nice ' ],
1872
+ ]
1873
+ ], ['associated ' => ['Articles ' ]]);
1874
+
1875
+ $ entity = $ authors ->save ($ entity , ['associated ' => ['Articles ' ]]);
1876
+
1877
+ $ this ->assertEquals (2 , $ authors ->Articles ->find ('all ' )->where (['author_id ' => $ entity ['id ' ]])->count ());
1878
+
1879
+ $ articleId = $ entity ->articles [0 ]->id ;
1880
+ unset($ entity ->articles [0 ]);
1881
+ $ entity ->dirty ('articles ' , true );
1882
+
1883
+ $ authors ->save ($ entity , ['associated ' => ['Articles ' ]]);
1884
+
1885
+ $ this ->assertEquals (1 , $ authors ->Articles ->find ('all ' )->where (['author_id ' => $ entity ['id ' ]])->count ());
1886
+ $ this ->assertFalse ($ authors ->Articles ->exists (['id ' => $ articleId ]));
1887
+ }
1888
+
1843
1889
/**
1844
1890
* Test that saving a new entity with a Primary Key set does not call exists when checkExisting is false.
1845
1891
*
0 commit comments