|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the WP_Plugin_Dependencies::get_circular_dependencies() method. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + */ |
| 7 | + |
| 8 | +require_once __DIR__ . '/base.php'; |
| 9 | + |
| 10 | +/** |
| 11 | + * @group admin |
| 12 | + * @group plugins |
| 13 | + * |
| 14 | + * @covers WP_Plugin_Dependencies::get_circular_dependencies |
| 15 | + * @covers WP_Plugin_Dependencies::check_for_circular_dependencies |
| 16 | + */ |
| 17 | +class Tests_Admin_WPPluginDependencies_GetCircularDependencies extends WP_PluginDependencies_UnitTestCase { |
| 18 | + |
| 19 | + /** |
| 20 | + * Tests that stored circular dependencies are returned without checking again. |
| 21 | + * |
| 22 | + * @ticket 65819 |
| 23 | + */ |
| 24 | + public function test_should_return_stored_circular_dependencies() { |
| 25 | + $this->set_dependencies( array( 'a' => array( 'a' ) ) ); |
| 26 | + $this->set_property_value( 'circular_dependencies_pairs', array( array( 'x', 'y' ) ) ); |
| 27 | + |
| 28 | + $this->assertSame( array( array( 'x', 'y' ) ), $this->call_method( 'get_circular_dependencies' ), 'The stored pairs should be returned.' ); |
| 29 | + $this->assertNull( $this->get_property_value( 'circular_dependencies_slugs' ), 'The slugs should not be computed.' ); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Tests that an empty array is returned before the dependencies are read. |
| 34 | + * |
| 35 | + * @ticket 65819 |
| 36 | + */ |
| 37 | + public function test_should_return_empty_array_without_dependencies() { |
| 38 | + $this->assertSame( array(), $this->call_method( 'get_circular_dependencies' ), 'No pairs should be returned.' ); |
| 39 | + $this->assertNull( $this->get_property_value( 'circular_dependencies_pairs' ), 'The pairs should not be stored.' ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Tests the circular dependency pairs and slugs found. |
| 44 | + * |
| 45 | + * @ticket 65819 |
| 46 | + * |
| 47 | + * @dataProvider data_dependencies |
| 48 | + * |
| 49 | + * @param array $dependencies The dependency slugs of each plugin, keyed by plugin slug. |
| 50 | + * @param array $pairs The expected pairs. |
| 51 | + * @param array $slugs The expected slugs, in any order. |
| 52 | + */ |
| 53 | + public function test_should_find_circular_dependencies( $dependencies, $pairs, $slugs ) { |
| 54 | + $this->set_dependencies( $dependencies ); |
| 55 | + |
| 56 | + $this->assertSame( $pairs, $this->call_method( 'get_circular_dependencies' ), 'The pairs should match.' ); |
| 57 | + $this->assertSame( $pairs, $this->get_property_value( 'circular_dependencies_pairs' ), 'The pairs should be stored.' ); |
| 58 | + $this->assertSameSets( $slugs, array_unique( $this->get_property_value( 'circular_dependencies_slugs' ) ), 'The slugs should match.' ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Data provider. |
| 63 | + * |
| 64 | + * @return array[] |
| 65 | + */ |
| 66 | + public static function data_dependencies() { |
| 67 | + return array( |
| 68 | + 'no circular dependency' => array( |
| 69 | + array( |
| 70 | + 'a' => array( 'b' ), |
| 71 | + 'b' => array( 'c' ), |
| 72 | + ), |
| 73 | + array(), |
| 74 | + array(), |
| 75 | + ), |
| 76 | + 'dependency that is not installed' => array( |
| 77 | + array( 'a' => array( 'missing' ) ), |
| 78 | + array(), |
| 79 | + array(), |
| 80 | + ), |
| 81 | + 'plugin that depends on itself' => array( |
| 82 | + array( 'a' => array( 'a' ) ), |
| 83 | + array( array( 'a', 'a' ) ), |
| 84 | + array( 'a' ), |
| 85 | + ), |
| 86 | + 'self-dependency next to another one' => array( |
| 87 | + array( |
| 88 | + 'a' => array( 'a', 'b' ), |
| 89 | + 'b' => array(), |
| 90 | + ), |
| 91 | + array( array( 'a', 'a' ) ), |
| 92 | + array( 'a' ), |
| 93 | + ), |
| 94 | + 'two plugins that depend on each other' => array( |
| 95 | + array( |
| 96 | + 'a' => array( 'b' ), |
| 97 | + 'b' => array( 'a' ), |
| 98 | + ), |
| 99 | + array( array( 'a', 'b' ), array( 'b', 'a' ) ), |
| 100 | + array( 'a', 'b' ), |
| 101 | + ), |
| 102 | + 'three plugins in a loop' => array( |
| 103 | + array( |
| 104 | + 'a' => array( 'b' ), |
| 105 | + 'b' => array( 'c' ), |
| 106 | + 'c' => array( 'a' ), |
| 107 | + ), |
| 108 | + array( array( 'a', 'c' ), array( 'b', 'a' ), array( 'c', 'b' ) ), |
| 109 | + array( 'a', 'b', 'c' ), |
| 110 | + ), |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Sets the dependencies, keyed by plugin slug, as read from the plugin headers. |
| 116 | + * |
| 117 | + * @param array $dependencies The dependency slugs of each plugin, keyed by plugin slug. |
| 118 | + */ |
| 119 | + private function set_dependencies( $dependencies ) { |
| 120 | + $by_file = array(); |
| 121 | + $dependents = array(); |
| 122 | + |
| 123 | + foreach ( $dependencies as $slug => $slugs ) { |
| 124 | + $file = "{$slug}/{$slug}.php"; |
| 125 | + |
| 126 | + $by_file[ $file ] = $slugs; |
| 127 | + $dependents[ $file ] = $slug; |
| 128 | + } |
| 129 | + |
| 130 | + $this->set_property_value( 'dependencies', $by_file ); |
| 131 | + $this->set_property_value( 'dependent_slugs', $dependents ); |
| 132 | + } |
| 133 | +} |
0 commit comments