Example #1
0
 /**
  * Exports the Craft datamodel.
  *
  * @param string $file    file to write the schema to
  * @param array  $exclude Data to not export
  *
  * @return int
  */
 public function actionIndex($file = 'craft/config/schema.yml', array $exclude = null)
 {
     $dataTypes = Schematic::getExportableDataTypes();
     // If there are data exclusions.
     if ($exclude !== null) {
         // Find any invalid data to exclude.
         $invalidExcludes = array_diff($exclude, $dataTypes);
         // If any invalid exclusions were specified.
         if (count($invalidExcludes) > 0) {
             $errorMessage = 'Invalid exlude';
             if (count($invalidExcludes) > 1) {
                 $errorMessage .= 's';
             }
             $errorMessage .= ': ' . implode(', ', $invalidExcludes) . '.';
             $errorMessage .= ' Valid exclusions are ' . implode(', ', $dataTypes);
             // Output an error message outlining what invalid exclusions were specified.
             echo "\n" . $errorMessage . "\n\n";
             return 1;
         }
         // Remove any explicitly excluded data types from the list of data types to export.
         $dataTypes = array_diff($dataTypes, $exclude);
     }
     Craft::app()->schematic->exportToYaml($file, $dataTypes);
     Craft::log(Craft::t('Exported schema to {file}', ['file' => $file]));
     return 0;
 }
 /**
  * @return ConsoleApp|WebApp
  */
 protected function getCraft()
 {
     if (!$this->craft) {
         $this->craft = Craft::app();
     }
     return $this->craft;
 }
Example #3
0
 /**
  * Import asset source definitions.
  *
  * @param array $assetSourceDefinitions
  * @param bool  $force
  *
  * @return Result
  */
 public function import(array $assetSourceDefinitions, $force = false)
 {
     Craft::log(Craft::t('Importing Asset Sources'));
     foreach ($assetSourceDefinitions as $assetHandle => $assetSourceDefinition) {
         $assetSource = $this->populateAssetSource($assetHandle, $assetSourceDefinition);
         if (!Craft::app()->assetSources->saveSource($assetSource)) {
             $this->addErrors($assetSource->getAllErrors());
         }
     }
     return $this->getResultModel();
 }
Example #4
0
 /**
  * @param array                $fieldDefinition
  * @param FieldModel           $field
  * @param string               $fieldHandle
  * @param FieldGroupModel|null $group
  */
 public function populate(array $fieldDefinition, FieldModel $field, $fieldHandle, FieldGroupModel $group = null)
 {
     parent::populate($fieldDefinition, $field, $fieldHandle, $group);
     $settings = $fieldDefinition['settings'];
     $defaultUploadLocationSourceId = $settings['defaultUploadLocationSource'];
     $defaultUploadLocationSource = Craft::app()->schematic_assetSources->getSourceTypeByHandle($defaultUploadLocationSourceId);
     $settings['defaultUploadLocationSource'] = $defaultUploadLocationSource ? $defaultUploadLocationSource->id : '';
     $singleUploadLocationSourceId = $settings['singleUploadLocationSource'];
     $singleUploadLocationSource = Craft::app()->schematic_assetSources->getSourceTypeByHandle($singleUploadLocationSourceId);
     $settings['singleUploadLocationSource'] = $singleUploadLocationSource ? $singleUploadLocationSource->id : '';
     $field->settings = $settings;
 }
Example #5
0
 /**
  * Imports the Craft datamodel.
  *
  * @param string $file          yml file containing the schema definition
  * @param string $override_file yml file containing the override values
  * @param bool   $force         if set to true items not in the import will be deleted
  *
  * @return int
  */
 public function actionIndex($file = 'craft/config/schema.yml', $override_file = 'craft/config/override.yml', $force = false)
 {
     if (!IOHelper::fileExists($file)) {
         $this->usageError(Craft::t('File not found.'));
     }
     $result = Craft::app()->schematic->importFromYaml($file, $override_file, $force);
     if (!$result->hasErrors()) {
         Craft::log(Craft::t('Loaded schema from {file}', ['file' => $file]));
         return 0;
     }
     Craft::log(Craft::t('There was an error loading schema from {file}', ['file' => $file]));
     print_r($result->getErrors());
     return 1;
 }
Example #6
0
 /**
  * Attempt to import user settings.
  *
  * @param array $user_settings
  * @param bool  $force         If set to true user settings not included in the import will be deleted
  *
  * @return Result
  */
 public function import(array $user_settings, $force = true)
 {
     Craft::log(Craft::t('Importing Users'));
     // always delete existing fieldlayout first
     Craft::app()->fields->deleteLayoutsByType(ElementType::User);
     if (isset($user_settings['fieldLayout'])) {
         $fieldLayoutDefinition = (array) $user_settings['fieldLayout'];
     } else {
         $fieldLayoutDefinition = [];
     }
     $fieldLayout = Craft::app()->schematic_fields->getFieldLayout($fieldLayoutDefinition);
     $fieldLayout->type = ElementType::User;
     if (!Craft::app()->fields->saveLayout($fieldLayout)) {
         // Save fieldlayout via craft
         $this->addErrors($fieldLayout->getAllErrors());
     }
     return $this->getResultModel();
 }
Example #7
0
 /**
  * Determines if Craft is installed by checking if the info table exists.
  *
  * @return bool
  */
 public function isInstalled()
 {
     if (!isset($this->_isInstalled)) {
         try {
             // First check to see if DbConnection has even been initialized, yet.
             if (Craft::app()->getComponent('db')) {
                 // If the db config isn't valid, then we'll assume it's not installed.
                 if (!Craft::app()->getIsDbConnectionValid()) {
                     return false;
                 }
             } else {
                 return false;
             }
         } catch (\Exception $e) {
             return false;
         }
         $this->_isInstalled = Craft::app()->db->tableExists('info', false);
     }
     return $this->_isInstalled;
 }
Example #8
0
 /**
  * Test users service import.
  *
  * @covers ::import
  */
 public function testUsersServiceImportWithImportError()
 {
     $mockFieldsService = $this->getMockFieldServiceForImport(false);
     $mockSchematicFieldsService = $this->getMockSchematicFieldServiceForImport(true);
     $this->setComponent(Craft::app(), 'fields', $mockFieldsService);
     $this->setComponent(Craft::app(), 'schematic_fields', $mockSchematicFieldsService);
     $import = $this->schematicUsersService->import([]);
     $this->assertTrue($import instanceof Result);
     $this->assertTrue($import->hasErrors('errors'));
 }
Example #9
0
 /**
  * Test export functionality.
  *
  * @covers ::export
  */
 public function testExport()
 {
     $data = $this->getLocaleData();
     $locales = [];
     foreach ($data as $id) {
         $locales[] = new LocaleModel($id);
     }
     $mockLocalizationService = $this->getMockLocalizationService($data, $locales);
     $this->setComponent(Craft::app(), 'i18n', $mockLocalizationService);
     $export = $this->schematicLocalesService->export();
     $this->assertEquals($data, $export);
 }
Example #10
0
 /**
  * @return Mock|DbConnection
  */
 private function setMockDbConnection()
 {
     $mockDbConnection = $this->getMockBuilder(DbConnection::class)->disableOriginalConstructor()->setMethods(['createCommand'])->getMock();
     $mockDbConnection->autoConnect = false;
     // Do not auto connect
     $mockDbCommand = $this->getMockDbCommand();
     $mockDbConnection->expects($this->any())->method('createCommand')->willReturn($mockDbCommand);
     Craft::app()->setComponent('db', $mockDbConnection);
     return $mockDbConnection;
 }
Example #11
0
 /**
  * @param AssetSourceModel $assetSource
  *
  * @return array
  */
 private function getAssetSourceDefinition(AssetSourceModel $assetSource)
 {
     return ['type' => $assetSource->type, 'name' => $assetSource->name, 'sortOrder' => $assetSource->sortOrder, 'settings' => $assetSource->settings, 'fieldLayout' => Craft::app()->schematic_fields->getFieldLayoutDefinition($assetSource->getFieldLayout())];
 }
Example #12
0
 /**
  * @param string $handle
  * @param Mock   $mock
  */
 private function setCraftComponent($handle, Mock $mock)
 {
     $this->setComponent(Craft::app(), $handle, $mock);
 }
Example #13
0
 /**
  * @param array $schematicFieldModels
  */
 private function setMockPluginsService(array $schematicFieldModels)
 {
     $mockPluginsService = $this->getMockBuilder(PluginsService::class)->disableOriginalConstructor()->getMock();
     $mockPluginsService->expects($this->exactly(1))->method('call')->with('registerSchematicFieldModels')->willReturn($schematicFieldModels);
     $this->setComponent(Craft::app(), 'plugins', $mockPluginsService);
 }
Example #14
0
 /**
  * Populate globalset.
  *
  * @param GlobalSetModel $globalSet
  * @param array          $globalSetDefinition
  * @param string         $globalSetHandle
  */
 private function populateGlobalSet(GlobalSetModel $globalSet, array $globalSetDefinition, $globalSetHandle)
 {
     $globalSet->setAttributes(['handle' => $globalSetHandle, 'name' => $globalSetDefinition['name']]);
     $fieldLayout = Craft::app()->schematic_fields->getFieldLayout($globalSetDefinition['fieldLayout']);
     $globalSet->setFieldLayout($fieldLayout);
 }
Example #15
0
 /**
  * Get a prepared fieldLayout for the craft assembleLayout function.
  *
  * @param array $fieldLayoutDef
  *
  * @return array
  */
 private function getPrepareFieldLayout(array $fieldLayoutDef)
 {
     $layoutFields = [];
     $requiredFields = [];
     foreach ($fieldLayoutDef as $fieldHandle => $required) {
         $field = Craft::app()->fields->getFieldByHandle($fieldHandle);
         if ($field instanceof FieldModel) {
             $layoutFields[] = $field->id;
             if ($required) {
                 $requiredFields[] = $field->id;
             }
         }
     }
     return ['fields' => $layoutFields, 'required' => $requiredFields];
 }
Example #16
0
 /**
  * @return DbConnection
  */
 protected function getDbService()
 {
     return Craft::app()->db;
 }
Example #17
0
 /**
  * Exports the Craft datamodel.
  *
  * @param string $file file to write the schema to
  *
  * @return int
  */
 public function actionIndex($file = 'craft/config/schema.yml')
 {
     Craft::app()->schematic->exportToYaml($file);
     Craft::log(Craft::t('Exported schema to {file}', ['file' => $file]));
     return 0;
 }
Example #18
0
 /**
  * Test export functionality.
  *
  * @covers ::export
  */
 public function testExport()
 {
     $mockBasePlugin = $this->getMockBasePlugin();
     $mockBasePlugin->isInstalled = true;
     $mockBasePlugin->isEnabled = true;
     $data = $this->getPluginsData();
     $mockBasePlugin->expects($this->any())->method('getSettings')->willReturn((object) ['attributes' => $data[$this->pluginHandle]['settings']]);
     $mockPluginsService = $this->getMockPluginsService();
     $mockPluginsService->expects($this->any())->method('getPlugins')->willReturn([$this->pluginHandle => $mockBasePlugin]);
     $this->setComponent(Craft::app(), 'plugins', $mockPluginsService);
     $export = $this->schematicPluginsService->export();
     $this->assertEquals($data, $export);
 }
Example #19
0
 /**
  * @return SuperTableService
  */
 private function getSuperTableService()
 {
     return Craft::app()->superTable;
 }
Example #20
0
 /**
  * Populate taggroup.
  *
  * @param TagGroupModel $tagGroup
  * @param array         $tagGroupDefinition
  * @param string        $tagGroupHandle
  */
 private function populateTagGroup(TagGroupModel $tagGroup, array $tagGroupDefinition, $tagGroupHandle)
 {
     $tagGroup->setAttributes(['handle' => $tagGroupHandle, 'name' => $tagGroupDefinition['name']]);
     $fieldLayout = Craft::app()->schematic_fields->getFieldLayout($tagGroupDefinition['fieldLayout']);
     $tagGroup->setFieldLayout($fieldLayout);
 }
Example #21
0
 /**
  * Always return the super user.
  *
  * @return Craft\UserModel
  */
 public function getUser()
 {
     return Craft::app()->users->getUserById(1);
 }
Example #22
0
 /**
  * Install Craft.
  */
 private function _installCraft()
 {
     $options = ['username' => getenv('CRAFT_USERNAME'), 'email' => getenv('CRAFT_EMAIL'), 'password' => getenv('CRAFT_PASSWORD'), 'siteName' => getenv('CRAFT_SITENAME'), 'siteUrl' => getenv('CRAFT_SITEURL'), 'locale' => getenv('CRAFT_LOCALE')];
     Craft::app()->install->run($options);
 }
Example #23
0
 /**
  * @return PluginsService
  */
 private function getPluginsService()
 {
     return Craft::app()->plugins;
 }
Example #24
0
 /**
  * @return UpdatesService
  */
 protected function getUpdatesService()
 {
     return Craft::app()->updates;
 }
Example #25
0
 /**
  * Returns `enabled` from login provider class by default, or `enabled` overidden by the config
  */
 public function getIsEnabled()
 {
     // get plugin settings
     $pluginSettings = \Craft\Craft::app()->plugins->getPlugin('social')->getSettings();
     $loginProviders = $pluginSettings->loginProviders;
     if (isset($loginProviders[$this->getHandle()]['enabled']) && $loginProviders[$this->getHandle()]['enabled']) {
         return true;
     }
     return false;
 }
 public function __get($memberName)
 {
     $craftName = 'market_' . strtolower($memberName);
     return Craft::app()->{$craftName};
 }
 /**
  * Test export functionality.
  *
  * @covers ::export
  */
 public function testExport()
 {
     $data = $this->getElementsData();
     $mockElementsService = $this->getMockElementsService($data);
     $this->setComponent(Craft::app(), 'elements', $mockElementsService);
     $data = $this->getElementIndexSettingsData();
     $mockElementIndexesService = $this->getMockElementIndexesService($data);
     $this->setComponent(Craft::app(), 'elementIndexes', $mockElementIndexesService);
     $export = $this->schematicElementIndexSettingsService->export();
     $this->assertEquals($data, $export);
 }
Example #28
0
 /**
  * Returns matrix service.
  *
  * @return MatrixService
  */
 private function getMatrixService()
 {
     return Craft::app()->matrix;
 }
 /**
  * @return ElementIndexesService
  */
 protected function getElementIndexesService()
 {
     return Craft::app()->elementIndexes;
 }
Example #30
0
 /**
  * @param array $permissions
  * @param bool  $success
  *
  * @return UserPermissionsService|Mock
  */
 private function setMockUserPermissionsService(array $permissions = [], $success = true)
 {
     $mockUserPermissionsService = $this->getMockBuilder(UserPermissionsService::class)->disableOriginalConstructor()->getMock();
     $mockUserPermissionsService->expects($this->any())->method('getAllPermissions')->willReturn($this->getAllPermissionsExample());
     $mockUserPermissionsService->expects($this->any())->method('getPermissionsByGroupId')->willReturnMap($permissions);
     $mockUserPermissionsService->expects($this->any())->method('saveGroupPermissions')->willReturn($success);
     $this->setComponent(Craft::app(), 'userPermissions', $mockUserPermissionsService);
     return $mockUserPermissionsService;
 }