Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ matrix:

services:
- mysql
# In Docker we will have CouchDB 2.2.0 on 127.0.0.1:3001.
# In Docker we will have CouchDB 2.3.0 on 127.0.0.1:3001.
- docker
# CouchDB 1.6.1 on 127.0.0.1:5984.
- couchdb
Expand All @@ -27,11 +27,11 @@ addons:
env:
matrix:
- SCRIPT='modules/relaxed/tests/bin/drupal.sh' MAKE_FILE=drupal-8.5.x.make.yml
- SCRIPT='modules/relaxed/tests/bin/replication.sh' MAKE_FILE=drupal-8.5.x.make.yml COUCHDB_VERSION=2.2.0
- SCRIPT='modules/relaxed/tests/bin/replication.sh' MAKE_FILE=drupal-8.5.x.make.yml COUCHDB_VERSION=2.3.0
- SCRIPT='modules/relaxed/tests/bin/replication.sh' MAKE_FILE=drupal-8.5.x.make.yml COUCHDB_VERSION=1.6.1
- SCRIPT='modules/relaxed/tests/bin/pouchdb.sh' MAKE_FILE=drupal-8.5.x.make.yml
- SCRIPT='modules/relaxed/tests/bin/drupal.sh' MAKE_FILE=drupal-8.6.x.make.yml
- SCRIPT='modules/relaxed/tests/bin/replication.sh' MAKE_FILE=drupal-8.6.x.make.yml COUCHDB_VERSION=2.2.0
- SCRIPT='modules/relaxed/tests/bin/replication.sh' MAKE_FILE=drupal-8.6.x.make.yml COUCHDB_VERSION=2.3.0
- SCRIPT='modules/relaxed/tests/bin/replication.sh' MAKE_FILE=drupal-8.6.x.make.yml COUCHDB_VERSION=1.6.1
- SCRIPT='modules/relaxed/tests/bin/pouchdb.sh' MAKE_FILE=drupal-8.6.x.make.yml

Expand Down
2 changes: 1 addition & 1 deletion .travis/drupal-8.5.x.make.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ projects:
type: module
download:
type: git
branch: 8.x-1.x
branch: selective-content-replication
url: "https://github.com/timmillwood/drupal-workspace.git"
2 changes: 1 addition & 1 deletion .travis/drupal-8.6.x.make.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ projects:
type: module
download:
type: git
branch: 8.x-1.x
branch: selective-content-replication
url: "https://github.com/timmillwood/drupal-workspace.git"
1 change: 1 addition & 0 deletions src/CouchdbReplicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function replicate(WorkspacePointerInterface $source, WorkspacePointerInt

if ($task !== NULL) {
$couchdb_task->setFilter($task->getFilter());
$couchdb_task->setDocIds($task->getDocIds());
$couchdb_task->setParameters($task->getParameters());
$changes_limit = \Drupal::config('replication.settings')->get('changes_limit');
$couchdb_task->setLimit($changes_limit ?: $task->getLimit());
Expand Down
2 changes: 1 addition & 1 deletion tests/bin/run-couchdb.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

if [ $COUCHDB_VERSION = "2.2.0" ]; then
if [ $COUCHDB_VERSION = "2.3.0" ]; then
docker run -d -p 3001:5984 apache/couchdb:$COUCHDB_VERSION --with-haproxy --with-admin-party-please -n 1
export COUCH_PORT=3001
elif [ $COUCHDB_VERSION = "1.6.1" ]; then
Expand Down
86 changes: 86 additions & 0 deletions tests/src/Integration/PhpSelectiveContentReplicatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Drupal\Tests\relaxed\Integration;

use Relaxed\Replicator\ReplicationTask;

require_once __DIR__ . '/ReplicationTestBase.php';

/**
* @group relaxed
*/
class PhpSelectiveContentReplicatorTest extends ReplicationTestBase {

/**
* Test the selective content replication using the PHP replicator.
*/
public function testPhpSelectiveContentReplication() {
$source_workspace = 'source';
$target_workspace = 'target';
$this->createWorkspace('http://replicator:replicator@localhost:8080/relaxed', $source_workspace);
$this->createWorkspace('http://replicator:replicator@localhost:8081/relaxed', $target_workspace);

// Run CouchDB to Drupal replication. Replicate all 9 docs (entities).
$this->assertAllDocsNumber("http://replicator:replicator@localhost:8080/relaxed/$source_workspace/_all_docs", 0);
$this->phpReplicate('{"source": {"dbname": "' . $this->sourceDb . '", "port": ' . $this->port . '}, "target": {"host": "localhost", "path": "relaxed", "port": 8080, "user": "replicator", "password": "replicator", "dbname": "' . $source_workspace . '", "timeout": 60}}');
$this->assertAllDocsNumber("http://replicator:replicator@localhost:8080/relaxed/$source_workspace/_all_docs", 9);

$task = new ReplicationTask();

// Replicate only 2 documents (2 taxonomy terms).
// @see /test/fixtures/documents.txt
$task->setDocIds([
'95615828-70db-v26b-9057-f6cc905dcn6h',
'77545828-70db-95gb-9057-f6553218dcn6',
]);

// Run Drupal to Drupal replication.
$this->assertAllDocsNumber("http://replicator:replicator@localhost:8081/relaxed/$target_workspace/_all_docs", 0);
$this->phpReplicate('{"source": {"host": "localhost", "path": "relaxed", "port": 8080, "user": "replicator", "password": "replicator", "dbname": "' . $source_workspace . '", "timeout": 60}, "target": {"host": "localhost", "path": "relaxed", "port": 8081, "user": "replicator", "password": "replicator", "dbname": "' . $target_workspace . '", "timeout": 60}}', $task);
$this->assertAllDocsNumber("http://replicator:replicator@localhost:8081/relaxed/$target_workspace/_all_docs", 2);

// Run Drupal to CouchDB replication.
$this->assertAllDocsNumber($this->couchdbUrl . '/' . $this->targetDb . '/_all_docs', 0);
$this->phpReplicate('{"source": {"host": "localhost", "path": "relaxed", "port": 8081, "user": "replicator", "password": "replicator", "dbname": "' . $target_workspace . '", "timeout": 60}, "target": {"dbname": "' . $this->targetDb . '", "port": ' . $this->port . '}}', $task);
$this->assertAllDocsNumber($this->couchdbUrl . '/' . $this->targetDb . '/_all_docs', 2);

// Replicate 3 more docs.
// @see /test/fixtures/documents.txt
$task->setDocIds([
'6f9e1f07-e713-4840-bf95-8326c8317800',
'ad3d5c67-e82a-4faf-a7fd-c5ad3975b622',
'1da2a674-4740-4edb-ad3d-2e243c9e6821',
]);

// Run Drupal to Drupal replication.
$this->phpReplicate('{"source": {"host": "localhost", "path": "relaxed", "port": 8080, "user": "replicator", "password": "replicator", "dbname": "' . $source_workspace . '", "timeout": 60}, "target": {"host": "localhost", "path": "relaxed", "port": 8081, "user": "replicator", "password": "replicator", "dbname": "' . $target_workspace . '", "timeout": 60}}', $task);
$this->assertAllDocsNumber("http://replicator:replicator@localhost:8081/relaxed/$target_workspace/_all_docs", 5);

// Run Drupal to CouchDB replication.
$this->phpReplicate('{"source": {"host": "localhost", "path": "relaxed", "port": 8081, "user": "replicator", "password": "replicator", "dbname": "' . $target_workspace . '", "timeout": 60}, "target": {"dbname": "' . $this->targetDb . '", "port": ' . $this->port . '}}', $task);
$this->assertAllDocsNumber($this->couchdbUrl . '/' . $this->targetDb . '/_all_docs', 5);

// Shouldn't replicate anything because of non-existing doc ID.
$task->setDocIds(['non-existing-doc-id']);

// Run Drupal to Drupal replication.
$this->phpReplicate('{"source": {"host": "localhost", "path": "relaxed", "port": 8080, "user": "replicator", "password": "replicator", "dbname": "' . $source_workspace . '", "timeout": 60}, "target": {"host": "localhost", "path": "relaxed", "port": 8081, "user": "replicator", "password": "replicator", "dbname": "' . $target_workspace . '", "timeout": 60}}', $task);
$this->assertAllDocsNumber("http://replicator:replicator@localhost:8081/relaxed/$target_workspace/_all_docs", 5);

// Run Drupal to CouchDB replication.
$this->phpReplicate('{"source": {"host": "localhost", "path": "relaxed", "port": 8081, "user": "replicator", "password": "replicator", "dbname": "' . $target_workspace . '", "timeout": 60}, "target": {"dbname": "' . $this->targetDb . '", "port": ' . $this->port . '}}', $task);
$this->assertAllDocsNumber($this->couchdbUrl . '/' . $this->targetDb . '/_all_docs', 5);

// Now it should replicate all remaining changes.
$task = new ReplicationTask();

// Run Drupal to Drupal replication.
$this->phpReplicate('{"source": {"host": "localhost", "path": "relaxed", "port": 8080, "user": "replicator", "password": "replicator", "dbname": "' . $source_workspace . '", "timeout": 60}, "target": {"host": "localhost", "path": "relaxed", "port": 8081, "user": "replicator", "password": "replicator", "dbname": "' . $target_workspace . '", "timeout": 60}}', $task);
$this->assertAllDocsNumber("http://replicator:replicator@localhost:8081/relaxed/$target_workspace/_all_docs", 9);

// Run Drupal to CouchDB replication.
$this->phpReplicate('{"source": {"host": "localhost", "path": "relaxed", "port": 8081, "user": "replicator", "password": "replicator", "dbname": "' . $target_workspace . '", "timeout": 60}, "target": {"dbname": "' . $this->targetDb . '", "port": ' . $this->port . '}}', $task);
$this->assertAllDocsNumber($this->couchdbUrl . '/' . $this->targetDb . '/_all_docs', 9);
}

}
28 changes: 26 additions & 2 deletions tests/src/Integration/ReplicationTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ protected function createDb($db_name) {
return $code;
}

/**
* Creates a new workspace.
*/
protected function createWorkspace($relaxed_root, $workspace_name) {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_HTTPGET => FALSE,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_URL => "$relaxed_root/$workspace_name",
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
],
]);

curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

return $code;
}

/**
* Creates delete a database.
*/
Expand All @@ -181,7 +203,7 @@ protected function deleteDb($db_name) {
/**
* Replicates content from source to target using the PHP replicator.
*/
protected function phpReplicate($data) {
protected function phpReplicate($data, $task = NULL) {
$json = json_decode($data, true);
if (json_last_error() != JSON_ERROR_NONE) {
throw new Exception('Invalid JSON.');
Expand All @@ -190,7 +212,9 @@ protected function phpReplicate($data) {
$source = CouchDBClient::create($json['source']);
$target = CouchDBClient::create($json['target']);

$task = new ReplicationTask(null, false, null, null, false, null, 10000, 10000, false, "all_docs", 0, 2, 2);
if (!$task) {
$task = new ReplicationTask(null, false, null, null, false, null, 10000, 10000, false, "all_docs", 0, 2, 2);
}
$replicator = new Replicator($source, $target, $task);

return $replicator->startReplication();
Expand Down