/** * Adds the records to the database and returns a new {@link DataList} * * @param GridField * @param SS_List * @return SS_List */ public function getManipulatedData(GridField $gridField, SS_List $dataList) { $state = $gridField->State->MockDataGenerator; $count = (string) $state->Count; if (!$count) { return $dataList; } $generator = new MockDataBuilder($gridField->getModelClass()); $ids = $generator->setCount($count)->setIncludeRelations($state->IncludeRelations)->setDownloadImages($state->DownloadImages === true)->generate(); foreach ($ids as $id) { $dataList->add($id); } return $dataList; }
/** * Runs a command on the {@link MockDataBuilder} object using options * defined in the request. * * @param string The command to run * @param string The class to create or update */ protected function runBuilderCommand($cmd, $className) { $count = $this->request->getVar('count') ?: 10; $parent = $this->request->getVar('parent'); $parentField = $this->request->getVar('parentField') ?: "ParentID"; try { $builder = MockDataBuilder::create($className); } catch (Exception $e) { echo $e->getMessage(); die; } $builder->setOnlyEmpty($this->request->getVar('onlyEmpty') === "false" ? false : true)->setDownloadImages($this->request->getVar('downloadImages') === "false" ? false : true)->setCount($count)->setParentIdentifier($parent ?: null)->setParentField($parentField); try { $builder->{$cmd}(); } catch (Exception $e) { echo $e->getMessage() . "\n\n"; die; } }
public function testGeneratorCreatesRecords() { MockDataBuilder::create("MockDataObjectTest_Person")->setCount(10)->setIncludeRelations(false)->generate(); $this->assertEquals(11, $this->getList()->count()); }
/** * Handles the creation of mock children with {@link MockDataBuilder} * * @param array $data The data passed in from the form * @param CMSForm $form The Form object that was used * @return SSViewer */ public function doAddMockChildren($data, $form) { $parentPage = SiteTree::get()->byID((int) $data['ID']); if (!$parentPage) { return false; } $className = isset($data['PageType']) ? $data['PageType'] : "Page"; $builder = new MockDataBuilder($className); $builder->setCount((int) $data['Count'])->setIncludeRelations((bool) $data['IncludeRelations'])->setDownloadImages((bool) $data['DownloadImages'])->setParentField("ParentID")->setParentObj($parentPage); try { $ids = $builder->generate(); } catch (Exception $e) { $form->sessionMessage($e->getMessage(), "bad"); return $this->redirectBack(); } $this->response->addHeader('X-Status', _t('MockData.CREATESUCCESS', 'Created {count} mock children under {title}', array('count' => $data['Count'], 'title' => $parentPage->Title))); $this->redirect(Controller::join_links(singleton('CMSPagesController')->Link())); return $this->getResponseNegotiator()->respond($this->request); }