/** * Test cascading delete with has many. * * @return void */ public function testCascadeDeleteCallbacks() { $config = ['dependent' => true, 'sourceTable' => $this->author, 'targetTable' => $this->article, 'conditions' => ['Articles.is_active' => true], 'cascadeCallbacks' => true]; $association = new HasMany('Articles', $config); $articleOne = new Entity(['id' => 2, 'title' => 'test']); $articleTwo = new Entity(['id' => 3, 'title' => 'testing']); $iterator = new \ArrayIterator([$articleOne, $articleTwo]); $query = $this->getMock('\\Cake\\ORM\\Query', [], [], '', false); $query->expects($this->at(0))->method('where')->with(['Articles.is_active' => true])->will($this->returnSelf()); $query->expects($this->at(1))->method('where')->with(['author_id' => 1])->will($this->returnSelf()); $query->expects($this->any())->method('getIterator')->will($this->returnValue($iterator)); $query->expects($this->never())->method('bufferResults'); $this->article->expects($this->once())->method('find')->will($this->returnValue($query)); $this->article->expects($this->at(1))->method('delete')->with($articleOne, []); $this->article->expects($this->at(2))->method('delete')->with($articleTwo, []); $entity = new Entity(['id' => 1, 'name' => 'mark']); $this->assertTrue($association->cascadeDelete($entity)); }
/** * Test cascading delete with has many. * * @return void */ public function testCascadeDeleteCallbacks() { $articles = TableRegistry::get('Articles'); $config = ['dependent' => true, 'sourceTable' => $this->author, 'targetTable' => $articles, 'conditions' => ['Articles.published' => 'Y'], 'cascadeCallbacks' => true]; $association = new HasMany('Articles', $config); $author = new Entity(['id' => 1, 'name' => 'mark']); $this->assertTrue($association->cascadeDelete($author)); $query = $articles->query()->where(['author_id' => 1]); $this->assertEquals(0, $query->count(), 'Cleared related rows'); $query = $articles->query()->where(['author_id' => 3]); $this->assertEquals(1, $query->count(), 'other records left behind'); }