/** * {@inheritDoc} */ public function pasteInto(ModelInterface $parentModel, CollectionInterface $models, $sortedBy) { $environment = $this->getEnvironment(); foreach ($models as $model) { $this->setParent($model, $parentModel); } // Enforce proper sorting now. $siblings = $this->assembleChildrenFor($parentModel, $sortedBy); $sortManager = new SortingManager($models, $siblings, $sortedBy); $newList = $sortManager->getResults(); $environment->getDataProvider($newList->get(0)->getProviderName())->saveEach($newList); }
/** * Test if the sorting is applied as expected. * * We do not test the specific sorting value, only make sure that * + is greater than the previous one * + that the actual and the previous one be between 2 and 128 * * @param array $siblings Create model for the sibling. * @param array $resortingIds Ids of items being resorted. * @param int|null $previousModelId Previous model id. * @param array $expectedOrder Expected order. * * @dataProvider provideTestData() */ public function testAppliedSorting(array $siblings, array $resortingIds, $previousModelId, array $expectedOrder) { $siblingCollection = new DefaultCollection(); $modelCollection = new DefaultCollection(); $previousModel = null; $this->prepareCollections($siblings, $resortingIds, $previousModelId, $siblingCollection, $modelCollection, $previousModel); $sortingManager = new SortingManager($modelCollection, $siblingCollection, 'sorting', $previousModel); $position = $previousModel ? $previousModel->getProperty('sorting') : null; $affected = $sortingManager->getResults()->getModelIds(); $ordered = array(); foreach ($expectedOrder as $id) { // Only compare if previous model is given and not identical with test model. // Only test affected items as well. if ($previousModel && $previousModel->getId() != $id && in_array($id, $affected)) { $this->assertGreaterThan($position, $siblings[$id]->getProperty('sorting')); $this->assertGreaterThanOrEqual(2, $siblings[$id]->getProperty('sorting') - $position); $this->assertLessThanOrEqual(128, $siblings[$id]->getProperty('sorting') - $position); } $previousModel = $siblings[$id]; $position = $previousModel->getProperty('sorting'); $ordered[$position] = $id; } // Explicit compare the new order with expected order. ksort($ordered); $this->assertEquals(array_values($ordered), $expectedOrder); }