function tesseract_import_packages($packages) { if (empty($packages)) { return; } $current_package_slugs_to_versions = array(); foreach ($packages as $package) { $slug = $package['details']['slug']; $version = $package['details']['version']; $current_package_slugs_to_versions[$slug] = $version; } $existing_packages = tesseract_get_previously_imported_packages(); // Get packages that need to be totally deleted $packages_for_deletion = array(); foreach ($existing_packages as $slug => $existing_package) { if (empty($current_package_slugs_to_versions[$slug])) { $packages_for_deletion[] = $slug; } } // Get packages that need to be added/updated $packages_for_importing = array(); $packages_for_updating = array(); foreach ($current_package_slugs_to_versions as $slug => $version) { // Check to see if this package slug & version have been imported. if (empty($existing_packages[$slug])) { // If not, do the import $packages_for_importing[] = $slug; } elseif ($existing_packages[$slug] != $version) { $packages_for_updating[] = $slug; } } foreach ($packages as $package) { $slug = $package['details']['slug']; if (in_array($slug, $packages_for_importing)) { tesseract_import_package($package); } elseif (in_array($slug, $packages_for_updating)) { tesseract_delete_package_with_slug($slug); tesseract_import_package($package); } } foreach ($existing_packages as $slug => $existing_package) { if (in_array($slug, $packages_for_deletion)) { tesseract_delete_package_with_slug($slug); } } }
function testPackageDeletion() { // Run importer with 2 packages $packages = tesseract_get_packages(dirname(__FILE__) . '/data/test-delete-package.json'); tesseract_import_packages($packages); // Assert the number of imported packages is 2 $prev_packages = tesseract_get_previously_imported_packages(); $this->assertEquals(2, count($prev_packages)); // Grab the slug for the second package for later use $second_package_slug = $packages[1]['details']['slug']; // Assert a post from the 2nd package is present $posts = tesseract_get_tracked_posts_from_package_slug($second_package_slug); $this->assertNotEmpty($posts); // Run importer with 1 package only unset($packages[1]); tesseract_import_packages($packages); // Assert number of imported packages is 1 $prev_packages = tesseract_get_previously_imported_packages(); $this->assertEquals(1, count($prev_packages)); // Assert that same post has been deleted $posts = tesseract_get_tracked_posts_from_package_slug($second_package_slug); $this->assertEmpty($posts); }