/** * @var bool * * @throws Exception */ public static function requireRecords($force = false) { if (self::$ran && !$force) { return true; } self::$ran = true; if (!(Director::isDev() || Director::isTest())) { throw new Exception('requireRecords can only be run in development or test environments'); } $factory = Injector::inst()->create('PopulateFactory'); foreach (self::config()->get('truncate_objects') as $objName) { $versions = array(); if (class_exists($objName)) { foreach (DataList::create($objName) as $obj) { // if the object has the versioned extension, make sure we delete // that as well if ($obj->hasExtension('Versioned')) { foreach ($obj->getVersionedStages() as $stage) { $versions[$stage] = true; $obj->deleteFromStage($stage); } } try { @$obj->delete(); } catch (Exception $e) { // notice } } } if ($versions) { self::truncate_versions($objName, $versions); } foreach ((array) ClassInfo::getValidSubClasses($objName) as $table) { self::truncate_table($table); self::truncate_versions($table, $versions); } self::truncate_table($objName); } foreach (self::config()->get('include_yaml_fixtures') as $fixtureFile) { $fixture = new YamlFixture($fixtureFile); $fixture->writeInto($factory); $fixture = null; } // hook allowing extensions to clean up records, modify the result or // export the data to a SQL file (for importing performance). $static = !(isset($this) && get_class($this) == __CLASS__); if ($static) { $populate = Injector::inst()->create('Populate'); } else { $populate = $this; } $populate->extend('onAfterPopulateRecords'); return true; }
function run($request = null) { if (!DataObject::get_one('ZonedShippingMethod')) { $factory = Injector::inst()->create('FixtureFactory'); $fixture = new YamlFixture('silvershop-shipping/tests/fixtures/ZonedShippingMethod.yml'); $fixture->writeInto($factory); DB::alteration_message('Created zoned shipping methods', 'created'); } else { DB::alteration_message('Some zoned shipping methods already exist. None were created.'); } }
/** * @var bool * * @throws Exception */ public static function requireRecords($force = false) { if (self::$ran && !$force) { return true; } self::$ran = true; if (!(Director::isDev() || Director::isTest())) { throw new Exception('requireRecords can only be run in development or test environments'); } $factory = Injector::inst()->create('PopulateFactory'); foreach (self::config()->get('truncate_objects') as $objName) { $versions = array(); if (class_exists($objName)) { foreach (DataList::create($objName) as $obj) { // if the object has the versioned extension, make sure we delete // that as well if ($obj->hasExtension('Versioned')) { foreach ($obj->getVersionedStages() as $stage) { $versions[$stage] = true; $obj->deleteFromStage($stage); } } try { @$obj->delete(); } catch (Exception $e) { // notice } } } if ($versions) { self::truncate_versions($objName, $versions); } foreach (ClassInfo::getValidSubClasses($objName) as $table) { self::truncate_table($table); self::truncate_versions($table, $versions); } self::truncate_table($objName); } foreach (self::config()->get('include_yaml_fixtures') as $fixtureFile) { $fixture = new YamlFixture($fixtureFile); $fixture->writeInto($factory); $fixture = null; } return true; }
/** * Accepts YAML fixture definitions similar to the ones used in SilverStripe unit testing. * * Example: Given there are the following member records: * member1: * Email: member1@test.com * member2: * Email: member2@test.com * * @Given /^there are the following ([^\s]*) records$/ */ public function stepThereAreTheFollowingRecords($dataObject, PyStringNode $string) { $yaml = array_merge(array($dataObject . ':'), $string->getLines()); $yaml = implode("\n ", $yaml); // Save fixtures into database // TODO Run prepareAsset() for each File and Folder record $yamlFixture = new \YamlFixture($yaml); $yamlFixture->writeInto($this->getFixtureFactory()); }
public function run($request) { if ($request->getVar('createintzone')) { $this->populateInternationalZone(); DB::alteration_message('Created an international zone', 'created'); return; } $this->extend("beforePopulate"); $factory = Injector::inst()->create('FixtureFactory'); $parentid = 0; //create products if (!DataObject::get_one('Product')) { $fixture = new YamlFixture(SHOP_DIR . "/tests/fixtures/dummyproducts.yml"); $fixture->writeInto($factory); //needs to be a data model $shoppage = ProductCategory::get()->filter('URLSegment', 'shop')->first(); $parentid = $shoppage->ID; $categoriestopublish = array('products', 'electronics', 'apparel', 'entertainment', 'music', 'movies', 'drama', 'toys', 'food', 'books', 'jewellery', 'furniture', 'kitchen', 'bedroom', 'stationery'); foreach ($categoriestopublish as $categoryname) { $factory->get("ProductCategory", $categoryname)->publish('Stage', 'Live'); } $productstopublish = array('mp3player', 'hdtv', 'socks', 'tshirt', 'beachball', 'hoop', 'kite', 'genericmovie', 'lemonchicken', 'ring', 'book', 'lamp', 'paper', 'pens'); foreach ($productstopublish as $productname) { $factory->get("Product", $productname)->publish('Stage', 'Live'); } DB::alteration_message('Created dummy products and categories', 'created'); } else { echo "<p style=\"color:orange;\">Products and categories were not created because some already exist.</p>"; } //cart page if (!($page = DataObject::get_one('CartPage'))) { $fixture = new YamlFixture(SHOP_DIR . "/tests/fixtures/pages/Cart.yml"); $fixture->writeInto($factory); $page = $factory->get("CartPage", "cart"); $page->ParentID = $parentid; $page->writeToStage('Stage'); $page->publish('Stage', 'Live'); DB::alteration_message('Cart page created', 'created'); } //checkout page if (!($page = DataObject::get_one('CheckoutPage'))) { $fixture = new YamlFixture(SHOP_DIR . "/tests/fixtures/pages/Checkout.yml"); $fixture->writeInto($factory); $page = $factory->get("CheckoutPage", "checkout"); $page->ParentID = $parentid; $page->writeToStage('Stage'); $page->publish('Stage', 'Live'); DB::alteration_message('Checkout page created', 'created'); } //account page if (!DataObject::get_one('AccountPage')) { $fixture = new YamlFixture(SHOP_DIR . "/tests/fixtures/pages/Account.yml"); $fixture->writeInto($factory); $page = $factory->get("AccountPage", "account"); $page->ParentID = $parentid; $page->writeToStage('Stage'); $page->publish('Stage', 'Live'); DB::alteration_message('Account page \'Account\' created', 'created'); } //terms page if (!($termsPage = DataObject::get_one('Page', "\"URLSegment\" = 'terms-and-conditions'"))) { $fixture = new YamlFixture(SHOP_DIR . "/tests/fixtures/pages/TermsConditions.yml"); $fixture->writeInto($factory); $page = $factory->get("Page", "termsconditions"); $page->ParentID = $parentid; $page->writeToStage('Stage'); $page->publish('Stage', 'Live'); //set terms page id in config $config = SiteConfig::current_site_config(); $config->TermsPageID = $page->ID; $config->write(); DB::alteration_message("Terms and conditions page created", 'created'); } //countries config - removes some countries $siteconfig = SiteConfig::current_site_config(); if (empty($siteconfig->AllowedCountries)) { $siteconfig->AllowedCountries = "AF,AL,DZ,AS,AD,AO,AG,AR,AM,AU,AT,AZ,BS,BH,\n\t\t\tBD,BB,BY,BE,BZ,BJ,BT,BO,BA,BW,BR,BN,BG,BF,BI,\n\t\t\tKH,CM,CA,CV,CF,TD,CL,CN,CO,KM,CG,CR,CI,HR,CU,\n\t\t\tCY,CZ,DK,DJ,DM,DO,EC,EG,SV,GQ,ER,EE,ET,FJ,FI,\n\t\t\tFR,GA,GM,GE,DE,GH,GR,GD,GT,GN,GW,GY,HT,HN,HK,\n\t\t\tHU,IS,IN,ID,IR,IQ,IE,IL,IT,JM,JP,JO,KZ,KE,KI,\n\t\t\tKP,KR,KW,KG,LA,LV,LB,LS,LR,LY,LI,LT,LU,MG,MW,\n\t\t\tMY,MV,ML,MT,MH,MR,MU,MX,FM,MD,MC,MN,MS,MA,MZ,\n\t\t\tMM,NA,NR,NP,NL,NZ,NI,NE,NG,NO,OM,PK,PW,PA,PG,\n\t\t\tPY,PE,PH,PL,PT,QA,RO,RU,RW,KN,LC,VC,WS,SM,ST,\n\t\t\tSA,SN,SC,SL,SG,SK,SI,SB,SO,ZA,ES,LK,SD,SR,SZ,\n\t\t\tSE,CH,SY,TJ,TZ,TH,TG,TO,TT,TN,TR,TM,TV,UG,UA,\n\t\t\tAE,GB,US,UY,UZ,VU,VE,VN,YE,YU,ZM,ZW"; $siteconfig->write(); } $this->extend("afterPopulate"); }