diff --git a/deploy.info b/deploy.info index f06f879..a5d9fa9 100644 --- a/deploy.info +++ b/deploy.info @@ -22,3 +22,6 @@ files[] = plugins/DeployProcessorMemory.inc files[] = plugins/DeployProcessorQueue.inc files[] = plugins/DeployServiceRestJSON.inc files[] = plugins/DeployServiceRestXML.inc + +files[] = deploy.test +files[] = deploy_test_case.php diff --git a/deploy.test b/deploy.test new file mode 100644 index 0000000..dfaa0bf --- /dev/null +++ b/deploy.test @@ -0,0 +1,37 @@ + 'Send one node.', + 'description' => 'Create one node on first server and deploy it to second server.', + 'group' => 'Deployment' + ); + } + + /** + * Test itself. + */ + function testDeployNode() { + // Create a node on the first server. + $title = $this->randomString(); + $this->drupalCreateNode(array('title' => $title)); + + // Deploy plan 'deploy_example_plan'. + $this->deployPlan('deploy_example_plan'); + + // Switch to second server and load deployed node. + $this->switchToServer2(); + $deployed_node = node_load(1); + $this->assertEqual($title, $deployed_node->title, t('Node has been deployed successfully.'), 'Deploy'); + + // We should end up in server 2 to do proper tearDown(). + } +} diff --git a/deploy_test_case.php b/deploy_test_case.php new file mode 100644 index 0000000..5ade9dc --- /dev/null +++ b/deploy_test_case.php @@ -0,0 +1,173 @@ +saveSettings(1); + + // Manually create a view from feature. + module_load_include('inc', 'deploy_example', 'deploy_example.views_default'); + $views = deploy_example_views_default_views(); + $view = array_pop($views); + views_save_view($view); + + // Manually create a deploy_endpoint from feature. + module_load_include('inc', 'deploy_example', 'deploy_example.deploy_endpoints'); + $endpoints = deploy_example_deploy_endpoints_default(); + $endpoint = current($endpoints); + $endpoint->export_type = FALSE; + ctools_export_crud_save('deploy_endpoints', $endpoint); + + // Manually create a deploy_plan from feature. + module_load_include('inc', 'deploy_example', 'deploy_example.deploy_plans'); + $plans = deploy_example_deploy_plans_default(); + $plan = current($plans); + $plan->export_type = FALSE; + ctools_export_crud_save('deploy_plans', $plan); + + // Get back to the original connection. + Database::renameConnection('default', 'simletest_server1'); + Database::renameConnection('simpletest_original_default', 'default'); + + // Presave some of the 'original' values as during the next parent::setUp() + // call they will be updated with wrong data. + $language = $this->originalLanguage; + $language_default = $this->originalLanguageDefault; + $original_directory = $this->originalFileDirectory; + $original_profile = $this->originalProfile; + $callbacks = $this->originalShutdownCallbacks; + + // Install second server. Here we should add what modules we would like + // to enable for second server. + parent::setUp('ctools', 'uuid', 'services', 'rest_server', 'features', 'uuid_services', 'uuid_services_example'); + $this->saveSettings(2); + + // Manually import services endpoint from uuid_services_example. + module_load_include('inc', 'uuid_services_example', 'uuid_services_example.services.inc'); + $services_endpoints = uuid_services_example_default_services_endpoint(); + $services_endpoint = current($services_endpoints); + $services_endpoint->export_type = FALSE; + services_endpoint_save($services_endpoint); + + // Switch back to server 1 in order to change deployment endpoint settings. + $this->switchToServer1(); + + // Restore 'original' values. + $this->originalLanguage = $language; + $this->originalLanguageDefault = $language_default; + $this->originalFileDirectory = $original_directory; + $this->originalProfile = $original_profile; + $this->originalShutdownCallbacks = $callbacks; + + $endpoint = deploy_endpoint_load('deploy_example_endpoint'); + // Change url of the endpoint. + $endpoint->service_config['url'] = url('api', array('absolute' => TRUE)); + // We need to set User Agent header for endpoint + // in order to get to sandbox server 2. + $user_agent = drupal_generate_test_ua($this->servers[2]['databasePrefix']); + $endpoint->service_config['headers'] = array('User-Agent' => $user_agent); + ctools_export_crud_save('deploy_endpoints', $endpoint); + } + + /** + * Custom implementation of tearDown(). + * + * Remove both sandboxes. + */ + function tearDown() { + // Cleanup server 2. + parent::tearDown(); + + // Cleanup server 1 manually. + // Delete temporary files directory. + file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($this->servers[1]['databasePrefix'], 10)); + // Remove database. + Database::setActiveConnection('simletest_server1'); + // Remove all prefixed tables (all the tables in the schema). + $schema = drupal_get_schema(NULL, TRUE); + foreach ($schema as $name => $table) { + db_drop_table($name); + } + Database::setActiveConnection('default'); + Database::removeConnection('simletest_server1'); + } + + /** + * Restore some $this object values. + * + * @param integer $i + * Number of the server (1 or 2). + */ + function restoreSettings($i) { + $this->cookieFile = $this->servers[$i]['cookieFile']; + $this->databasePrefix = $this->servers[$i]['databasePrefix']; + $this->curlHandle = $this->servers[$i]['curlHandle']; + $this->cookieFile = $this->servers[$i]['cookieFile']; + } + + /** + * Presave some variables. + * + * @param integer $i + * Number of server (1 or 2). + */ + function saveSettings($i) { + if (empty($i)) { + return; + } + $this->servers[$i]['cookieFile'] = $this->cookieFile; + $this->servers[$i]['databasePrefix'] = $this->databasePrefix; + $this->servers[$i]['curlHandle'] = $this->curlHandle; + $this->servers[$i]['cookieFile'] = $this->cookieFile; + } + + /** + * Switch to server 1. + */ + function switchToServer1() { + Database::renameConnection('default', 'simletest_server2'); + Database::renameConnection('simletest_server1', 'default'); + $this->restoreSettings(1); + } + + /** + * Switch to server 2. + */ + function switchToServer2() { + Database::renameConnection('default', 'simletest_server1'); + Database::renameConnection('simletest_server2', 'default'); + $this->restoreSettings(2); + } + + /** + * Deploy the plan. + * + * @param string $name + * Name of the deployment plan. + * @return type + */ + function deployPlan($name) { + if (empty($name)) { + return; + } + + // Load the plan and deploy it so newly created node should be sent to server 2. + $deployment_plan = deploy_plan_load($name); + // Unserialize configuration arrays. + $deployment_plan->aggregator_config = unserialize($deployment_plan->aggregator_config); + $deployment_plan->processor_config = unserialize($deployment_plan->processor_config); + $deployment_plan->endpoints = unserialize($deployment_plan->endpoints); + $deployment_plan->deploy(); + } +} diff --git a/includes/DeployServiceRest.inc b/includes/DeployServiceRest.inc index 2d47938..d129fc9 100644 --- a/includes/DeployServiceRest.inc +++ b/includes/DeployServiceRest.inc @@ -12,6 +12,12 @@ class DeployServiceRest implements DeployService { var $headers = array(); function __construct(Array $config = array()) { + // Save headers from config. + if (isset($config['headers'])) { + $this->headers = $config['headers']; + unset($config['headers']); + } + $this->config += array( 'debug' => FALSE, 'url' => '',