From 51deb734c34a7c438174bb4e03b30396747ed8eb Mon Sep 17 00:00:00 2001
From: Wojciech Wozniak <wwozniak92@gmail.com>
Date: Tue, 14 Nov 2017 17:11:27 +0100
Subject: [PATCH 1/9] Link command in easyimage context menu.

---
 plugins/easyimage/plugin.js | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/plugins/easyimage/plugin.js b/plugins/easyimage/plugin.js
index 4dbd51c6c25..53883111139 100644
--- a/plugins/easyimage/plugin.js
+++ b/plugins/easyimage/plugin.js
@@ -73,7 +73,7 @@
 
 	function addMenuItems( editor ) {
 		editor.addMenuGroup( 'easyimage' );
-		editor.addMenuItems( {
+		var itemsData = {
 			easyimageFull: {
 				label: editor.lang.easyimage.commands.fullImage,
 				command: 'easyimageFull',
@@ -94,7 +94,16 @@
 				group: 'easyimage',
 				order: 3
 			}
-		} );
+		};
+		if ( editor.plugins.link ) {
+			itemsData.link = {
+				label: editor.lang.link.toolbar,
+				command: 'link',
+				group: 'easyimage',
+				order: 4
+			};
+		}
+		editor.addMenuItems( itemsData );
 	}
 
 	function getInitialImageType( widget ) {
@@ -135,6 +144,9 @@
 						evt.data.easyimageFull = editor.getCommand( 'easyimageFull' ).state;
 						evt.data.easyimageSide = editor.getCommand( 'easyimageSide' ).state;
 						evt.data.easyimageAlt = editor.getCommand( 'easyimageAlt' ).state;
+						if ( editor.plugins.link ) {
+							evt.data.link = editor.getCommand( 'link' ).state;
+						}
 					} );
 
 					if ( editor.config.easyimage_class ) {

From 51b1525bf09adda479190e8c23a25f12ed572803 Mon Sep 17 00:00:00 2001
From: Wojciech Wozniak <wwozniak92@gmail.com>
Date: Tue, 14 Nov 2017 17:39:09 +0100
Subject: [PATCH 2/9] Manual test for link context menu

---
 .../easyimage/manual/linkcontextmenu.html        | 16 ++++++++++++++++
 .../plugins/easyimage/manual/linkcontextmenu.md  | 11 +++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 tests/plugins/easyimage/manual/linkcontextmenu.html
 create mode 100644 tests/plugins/easyimage/manual/linkcontextmenu.md

diff --git a/tests/plugins/easyimage/manual/linkcontextmenu.html b/tests/plugins/easyimage/manual/linkcontextmenu.html
new file mode 100644
index 00000000000..9ec4e5d2d26
--- /dev/null
+++ b/tests/plugins/easyimage/manual/linkcontextmenu.html
@@ -0,0 +1,16 @@
+
+<div id="classic">
+	<p>Context menu should have option link: </p>
+	<figure class="easyimage">
+		<img src="../../image2/_assets/foo.png" alt="foo">
+		<figcaption>Test image</figcaption>
+	</figure>
+</div>
+
+<script>
+	CKEDITOR.replace( 'classic', {
+		easyimage_class: null,
+		height: 500,
+		extraAllowedContent: 'figure(*)'
+	} );
+</script>
diff --git a/tests/plugins/easyimage/manual/linkcontextmenu.md b/tests/plugins/easyimage/manual/linkcontextmenu.md
new file mode 100644
index 00000000000..939c5a4d5ce
--- /dev/null
+++ b/tests/plugins/easyimage/manual/linkcontextmenu.md
@@ -0,0 +1,11 @@
+@bender-tags: 4.8.0, feature, 932
+@bender-ui: collapsed
+@bender-ckeditor-plugins: sourcearea, wysiwygarea, floatingspace, toolbar, easyimage, link
+
+
+1. Right click to to add link.
+1. Click "Source" button to check output.
+
+## Expected
+
+* There is created link.

From 0a8100b1b6599220412c490c7d7f681d65b73870 Mon Sep 17 00:00:00 2001
From: Wojciech Wozniak <wwozniak92@gmail.com>
Date: Tue, 14 Nov 2017 17:59:29 +0100
Subject: [PATCH 3/9] unit test for link option in context menu

---
 tests/plugins/easyimage/linkcommand.js | 53 ++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 tests/plugins/easyimage/linkcommand.js

diff --git a/tests/plugins/easyimage/linkcommand.js b/tests/plugins/easyimage/linkcommand.js
new file mode 100644
index 00000000000..19fe3226c0e
--- /dev/null
+++ b/tests/plugins/easyimage/linkcommand.js
@@ -0,0 +1,53 @@
+/* bender-tags: editor,widget */
+/* bender-ckeditor-plugins: easyimage,toolbar, link */
+
+( function() {
+	'use strict';
+
+	bender.editors = {
+		classic: {},
+
+		divarea: {
+			config: {
+				extraPlugins: 'divarea'
+			}
+		},
+
+		inline: {
+			creator: 'inline'
+		}
+	};
+
+
+
+	var widgetHtml = '<figure class="image easyimage"><img src="../image2/_assets/foo.png" alt="foo"><figcaption>Test image</figcaption></figure>',
+		tests = {
+			tearDown: function() {
+				var currentDialog = CKEDITOR.dialog.getCurrent();
+
+				if ( currentDialog ) {
+					currentDialog.hide();
+				}
+			},
+
+			'test link option in context menu': function( editor, bot ) {
+				bot.setData( widgetHtml, function() {
+					var widget = editor.widgets.getByElement( editor.editable().findOne( 'figure' ) );
+
+					widget.focus();
+					editor.contextMenu.open( editor.editable() );
+					var itemsExist = 0;
+					for ( var i = 0; i < editor.contextMenu.items.length; ++i )
+						if ( editor.contextMenu.items[ i ].command == 'link' )
+							itemsExist += 1;
+
+					editor.contextMenu.hide();
+
+					assert.areSame( 1, itemsExist, 'there is one link item in context menu' );
+				} );
+			}
+		};
+
+	tests = bender.tools.createTestsForEditors( CKEDITOR.tools.objectKeys( bender.editors ), tests );
+	bender.test( tests );
+} )();

From 6240957bcb7ac295c135e3c9ece441202ba70e9d Mon Sep 17 00:00:00 2001
From: Wojciech Wozniak <wwozniak92@gmail.com>
Date: Thu, 16 Nov 2017 17:45:05 +0100
Subject: [PATCH 4/9] Link in contextmenu support for inline widget.

---
 plugins/easyimage/plugin.js                   | 19 ++++--------
 plugins/imagebase/plugin.js                   | 24 +++++++++++++++
 .../easyimage/manual/linkcontextmenu.html     | 16 ----------
 .../features}/linkcommand.js                  | 12 ++++++--
 .../features/manual/linkcontextmenu.html      | 29 +++++++++++++++++++
 .../features}/manual/linkcontextmenu.md       | 10 ++++++-
 6 files changed, 76 insertions(+), 34 deletions(-)
 delete mode 100644 tests/plugins/easyimage/manual/linkcontextmenu.html
 rename tests/plugins/{easyimage => imagebase/features}/linkcommand.js (69%)
 create mode 100644 tests/plugins/imagebase/features/manual/linkcontextmenu.html
 rename tests/plugins/{easyimage => imagebase/features}/manual/linkcontextmenu.md (54%)

diff --git a/plugins/easyimage/plugin.js b/plugins/easyimage/plugin.js
index 53883111139..ae51346ebdf 100644
--- a/plugins/easyimage/plugin.js
+++ b/plugins/easyimage/plugin.js
@@ -73,7 +73,7 @@
 
 	function addMenuItems( editor ) {
 		editor.addMenuGroup( 'easyimage' );
-		var itemsData = {
+		editor.addMenuItems( {
 			easyimageFull: {
 				label: editor.lang.easyimage.commands.fullImage,
 				command: 'easyimageFull',
@@ -94,16 +94,7 @@
 				group: 'easyimage',
 				order: 3
 			}
-		};
-		if ( editor.plugins.link ) {
-			itemsData.link = {
-				label: editor.lang.link.toolbar,
-				command: 'link',
-				group: 'easyimage',
-				order: 4
-			};
-		}
-		editor.addMenuItems( itemsData );
+		} );
 	}
 
 	function getInitialImageType( widget ) {
@@ -144,9 +135,9 @@
 						evt.data.easyimageFull = editor.getCommand( 'easyimageFull' ).state;
 						evt.data.easyimageSide = editor.getCommand( 'easyimageSide' ).state;
 						evt.data.easyimageAlt = editor.getCommand( 'easyimageAlt' ).state;
-						if ( editor.plugins.link ) {
-							evt.data.link = editor.getCommand( 'link' ).state;
-						}
+						// if ( editor.plugins.link ) {
+						// 	evt.data.link = editor.getCommand( 'link' ).state;
+						// }
 					} );
 
 					if ( editor.config.easyimage_class ) {
diff --git a/plugins/imagebase/plugin.js b/plugins/imagebase/plugin.js
index c080875229d..4ce10c3f33d 100644
--- a/plugins/imagebase/plugin.js
+++ b/plugins/imagebase/plugin.js
@@ -54,12 +54,36 @@
 			parts: {
 				link: 'a'
 			},
+			init: function() {
+				if ( this.editor.plugins.link ) {
+					this.on( 'contextMenu', function( evt ) {
+						evt.data.link = this.editor.getCommand( 'link' ).state;
+						evt.data.unlink = this.editor.getCommand( 'unlink' ).state;
+					} );
+				}
+			},
 
 			setUp: function( editor ) {
 				if ( !editor.plugins.link ) {
 					// All of listeners registered later on make only sense when link plugin is loaded.
 					return;
 				}
+				if ( editor.addMenuItems ) {
+					editor.addMenuItems( {
+						link: {
+							label: editor.lang.link.menu,
+							command: 'link',
+							group: 'imagebase',
+							order: 1
+						},
+						unlink: {
+							label: editor.lang.link.unlink,
+							command: 'unlink',
+							group: 'imagebase',
+							order: 2
+						}
+					} );
+				}
 
 				editor.on( 'dialogShow', function( evt ) {
 					var widget = getFocusedWidget( editor ),
diff --git a/tests/plugins/easyimage/manual/linkcontextmenu.html b/tests/plugins/easyimage/manual/linkcontextmenu.html
deleted file mode 100644
index 9ec4e5d2d26..00000000000
--- a/tests/plugins/easyimage/manual/linkcontextmenu.html
+++ /dev/null
@@ -1,16 +0,0 @@
-
-<div id="classic">
-	<p>Context menu should have option link: </p>
-	<figure class="easyimage">
-		<img src="../../image2/_assets/foo.png" alt="foo">
-		<figcaption>Test image</figcaption>
-	</figure>
-</div>
-
-<script>
-	CKEDITOR.replace( 'classic', {
-		easyimage_class: null,
-		height: 500,
-		extraAllowedContent: 'figure(*)'
-	} );
-</script>
diff --git a/tests/plugins/easyimage/linkcommand.js b/tests/plugins/imagebase/features/linkcommand.js
similarity index 69%
rename from tests/plugins/easyimage/linkcommand.js
rename to tests/plugins/imagebase/features/linkcommand.js
index 19fe3226c0e..83aa4c8733c 100644
--- a/tests/plugins/easyimage/linkcommand.js
+++ b/tests/plugins/imagebase/features/linkcommand.js
@@ -1,5 +1,5 @@
 /* bender-tags: editor,widget */
-/* bender-ckeditor-plugins: easyimage,toolbar, link */
+/* bender-ckeditor-plugins: imagebase,toolbar, link,contextmenu */
 
 ( function() {
 	'use strict';
@@ -19,8 +19,7 @@
 	};
 
 
-
-	var widgetHtml = '<figure class="image easyimage"><img src="../image2/_assets/foo.png" alt="foo"><figcaption>Test image</figcaption></figure>',
+	var widgetHtml = '<figure><img src="%BASE_PATH%_assets/logo.png"></figure>',
 		tests = {
 			tearDown: function() {
 				var currentDialog = CKEDITOR.dialog.getCurrent();
@@ -29,6 +28,13 @@
 					currentDialog.hide();
 				}
 			},
+			setUp: function() {
+				var plugin = CKEDITOR.plugins.imagebase,
+					editors = this.editors;
+				CKEDITOR.tools.array.forEach( CKEDITOR.tools.objectKeys( editors ), function( editor ) {
+					plugin.addImageWidget( editors[ editor ], 'testWidget', plugin.addFeature( editors[ editor ], 'link', {} ) );
+				} );
+			},
 
 			'test link option in context menu': function( editor, bot ) {
 				bot.setData( widgetHtml, function() {
diff --git a/tests/plugins/imagebase/features/manual/linkcontextmenu.html b/tests/plugins/imagebase/features/manual/linkcontextmenu.html
new file mode 100644
index 00000000000..bb66ee43500
--- /dev/null
+++ b/tests/plugins/imagebase/features/manual/linkcontextmenu.html
@@ -0,0 +1,29 @@
+
+<div id="classic">
+	<p>Widget with no link:</p>
+	<figure>
+		<img src="../../../image2/_assets/foo.png" alt="foo">
+		<figcaption>Test image</figcaption>
+	</figure>
+	<p>Widget with predefined link:</p>
+	<figure>
+		<a href="https://cksource.com">
+			<img src="../../../image2/_assets/foo.png" alt="foo">
+		</a>
+		<figcaption>Test image</figcaption>
+	</figure>
+</div>
+
+<script>
+	CKEDITOR.replace( 'classic', {
+		on: {
+			pluginsLoaded: function( evt ) {
+				var editor = evt.editor,
+					plugin = CKEDITOR.plugins.imagebase;
+
+				plugin.addImageWidget( editor, 'testWidget', plugin.addFeature( editor, 'link', {} ) );
+			}
+		},
+		height: 500
+	} );
+</script>
diff --git a/tests/plugins/easyimage/manual/linkcontextmenu.md b/tests/plugins/imagebase/features/manual/linkcontextmenu.md
similarity index 54%
rename from tests/plugins/easyimage/manual/linkcontextmenu.md
rename to tests/plugins/imagebase/features/manual/linkcontextmenu.md
index 939c5a4d5ce..8f7f51183fa 100644
--- a/tests/plugins/easyimage/manual/linkcontextmenu.md
+++ b/tests/plugins/imagebase/features/manual/linkcontextmenu.md
@@ -1,6 +1,6 @@
 @bender-tags: 4.8.0, feature, 932
 @bender-ui: collapsed
-@bender-ckeditor-plugins: sourcearea, wysiwygarea, floatingspace, toolbar, easyimage, link
+@bender-ckeditor-plugins: sourcearea, wysiwygarea, floatingspace, toolbar, imagebase, link, htmlwriter, elementspath, contextmenu
 
 
 1. Right click to to add link.
@@ -9,3 +9,11 @@
 ## Expected
 
 * There is created link.
+
+
+1. Try to remove link with context menu.
+1. Click "Source" button to check output.
+
+## Expected
+
+* The link should be reomved.

From 70bce952a2e98c5a20582044ce4bbb5d38f78cb6 Mon Sep 17 00:00:00 2001
From: Wojciech Wozniak <wwozniak92@gmail.com>
Date: Mon, 20 Nov 2017 13:08:38 +0100
Subject: [PATCH 5/9] Unit test updates and UX fix in link command for base
 image.

---
 plugins/easyimage/plugin.js                   |  3 -
 plugins/imagebase/plugin.js                   | 28 ++++-----
 tests/plugins/imagebase/features/link.js      | 59 ++++++++++++++++++-
 .../plugins/imagebase/features/linkcommand.js | 59 -------------------
 4 files changed, 69 insertions(+), 80 deletions(-)
 delete mode 100644 tests/plugins/imagebase/features/linkcommand.js

diff --git a/plugins/easyimage/plugin.js b/plugins/easyimage/plugin.js
index ae51346ebdf..4dbd51c6c25 100644
--- a/plugins/easyimage/plugin.js
+++ b/plugins/easyimage/plugin.js
@@ -135,9 +135,6 @@
 						evt.data.easyimageFull = editor.getCommand( 'easyimageFull' ).state;
 						evt.data.easyimageSide = editor.getCommand( 'easyimageSide' ).state;
 						evt.data.easyimageAlt = editor.getCommand( 'easyimageAlt' ).state;
-						// if ( editor.plugins.link ) {
-						// 	evt.data.link = editor.getCommand( 'link' ).state;
-						// }
 					} );
 
 					if ( editor.config.easyimage_class ) {
diff --git a/plugins/imagebase/plugin.js b/plugins/imagebase/plugin.js
index 4ce10c3f33d..11529c5a979 100644
--- a/plugins/imagebase/plugin.js
+++ b/plugins/imagebase/plugin.js
@@ -55,10 +55,11 @@
 				link: 'a'
 			},
 			init: function() {
-				if ( this.editor.plugins.link ) {
+				if ( this.editor.plugins.link && this.editor.contextMenu ) {
 					this.on( 'contextMenu', function( evt ) {
-						evt.data.link = this.editor.getCommand( 'link' ).state;
-						evt.data.unlink = this.editor.getCommand( 'unlink' ).state;
+						if ( this.parts.link || this.wrapper.getAscendant( 'a' ) ) {
+							evt.data.link = evt.data.unlink = CKEDITOR.TRISTATE_OFF;
+						}
 					} );
 				}
 			},
@@ -68,20 +69,13 @@
 					// All of listeners registered later on make only sense when link plugin is loaded.
 					return;
 				}
-				if ( editor.addMenuItems ) {
-					editor.addMenuItems( {
-						link: {
-							label: editor.lang.link.menu,
-							command: 'link',
-							group: 'imagebase',
-							order: 1
-						},
-						unlink: {
-							label: editor.lang.link.unlink,
-							command: 'unlink',
-							group: 'imagebase',
-							order: 2
-						}
+				if ( editor.contextMenu ) {
+					editor.addMenuGroup( 'imagebase', 10 );
+
+					editor.addMenuItem( 'imagebase', {
+						label: editor.lang.link.menu,
+						command: 'link',
+						group: 'imagebase'
 					} );
 				}
 
diff --git a/tests/plugins/imagebase/features/link.js b/tests/plugins/imagebase/features/link.js
index 32d4817f277..9ede25f986b 100644
--- a/tests/plugins/imagebase/features/link.js
+++ b/tests/plugins/imagebase/features/link.js
@@ -1,5 +1,5 @@
 /* bender-tags: editor,widget */
-/* bender-ckeditor-plugins: imagebase,link,toolbar */
+/* bender-ckeditor-plugins: imagebase,link,toolbar,contextmenu */
 /* bender-include: ../../widget/_helpers/tools.js */
 /* global widgetTestsTools */
 
@@ -389,6 +389,63 @@
 					} );
 				}
 			} );
+		},
+		'test link option in context menu': function( editor, bot ) {
+			addTestWidget( editor );
+			bot.setData( '<figure><a href="http://foo"><img src="%BASE_PATH%_assets/logo.png"></a></figure>', function() {
+				var widget = editor.widgets.getByElement( editor.editable().findOne( 'figure' ) );
+
+				widget.focus();
+				editor.contextMenu.open( editor.editable() );
+				var itemsExist = 0;
+				for ( var i = 0; i < editor.contextMenu.items.length; ++i ) {
+					if ( editor.contextMenu.items[ i ].command == 'link' ) {
+						itemsExist += 1;
+					}
+				}
+
+				editor.contextMenu.hide();
+
+				assert.areSame( 1, itemsExist, 'there is one link item in context menu' );
+			} );
+		},
+		'test unlink option in context menu': function( editor, bot ) {
+			addTestWidget( editor );
+			bot.setData( '<figure><a href="http://foo"><img src="%BASE_PATH%_assets/logo.png"></a></figure>', function() {
+				var widget = editor.widgets.getByElement( editor.editable().findOne( 'figure' ) );
+
+				widget.focus();
+				editor.contextMenu.open( editor.editable() );
+				var itemsExist = 0;
+				for ( var i = 0; i < editor.contextMenu.items.length; ++i ) {
+					if ( editor.contextMenu.items[ i ].command == 'unlink' ) {
+						itemsExist += 1;
+					}
+				}
+
+				editor.contextMenu.hide();
+
+				assert.areSame( 1, itemsExist, 'there is one link item in context menu' );
+			} );
+		},
+		'test no link option in context menu': function( editor, bot ) {
+			addTestWidget( editor );
+			bot.setData( '<figure><img src="%BASE_PATH%_assets/logo.png"></figure>', function() {
+				var widget = editor.widgets.getByElement( editor.editable().findOne( 'figure' ) );
+
+				widget.focus();
+				editor.contextMenu.open( editor.editable() );
+				var itemsExist = 0;
+				for ( var i = 0; i < editor.contextMenu.items.length; ++i ) {
+					if ( editor.contextMenu.items[ i ].command == 'unlink' || editor.contextMenu.items[ i ].command == 'link' ) {
+						itemsExist += 1;
+					}
+				}
+
+				editor.contextMenu.hide();
+
+				assert.areSame( 0, itemsExist, 'there should not be any link option in contextmenu' );
+			} );
 		}
 	};
 
diff --git a/tests/plugins/imagebase/features/linkcommand.js b/tests/plugins/imagebase/features/linkcommand.js
deleted file mode 100644
index 83aa4c8733c..00000000000
--- a/tests/plugins/imagebase/features/linkcommand.js
+++ /dev/null
@@ -1,59 +0,0 @@
-/* bender-tags: editor,widget */
-/* bender-ckeditor-plugins: imagebase,toolbar, link,contextmenu */
-
-( function() {
-	'use strict';
-
-	bender.editors = {
-		classic: {},
-
-		divarea: {
-			config: {
-				extraPlugins: 'divarea'
-			}
-		},
-
-		inline: {
-			creator: 'inline'
-		}
-	};
-
-
-	var widgetHtml = '<figure><img src="%BASE_PATH%_assets/logo.png"></figure>',
-		tests = {
-			tearDown: function() {
-				var currentDialog = CKEDITOR.dialog.getCurrent();
-
-				if ( currentDialog ) {
-					currentDialog.hide();
-				}
-			},
-			setUp: function() {
-				var plugin = CKEDITOR.plugins.imagebase,
-					editors = this.editors;
-				CKEDITOR.tools.array.forEach( CKEDITOR.tools.objectKeys( editors ), function( editor ) {
-					plugin.addImageWidget( editors[ editor ], 'testWidget', plugin.addFeature( editors[ editor ], 'link', {} ) );
-				} );
-			},
-
-			'test link option in context menu': function( editor, bot ) {
-				bot.setData( widgetHtml, function() {
-					var widget = editor.widgets.getByElement( editor.editable().findOne( 'figure' ) );
-
-					widget.focus();
-					editor.contextMenu.open( editor.editable() );
-					var itemsExist = 0;
-					for ( var i = 0; i < editor.contextMenu.items.length; ++i )
-						if ( editor.contextMenu.items[ i ].command == 'link' )
-							itemsExist += 1;
-
-					editor.contextMenu.hide();
-
-					assert.areSame( 1, itemsExist, 'there is one link item in context menu' );
-				} );
-			}
-		};
-
-	tests = bender.tools.createTestsForEditors( CKEDITOR.tools.objectKeys( bender.editors ), tests );
-	bender.test( tests );
-} )();

From f1d5b7802f4a47f4c5b5b0c9abf3ccce2d948961 Mon Sep 17 00:00:00 2001
From: Wojciech Wozniak <wwozniak92@gmail.com>
Date: Thu, 14 Dec 2017 14:42:42 +0100
Subject: [PATCH 6/9] Unit test clean up.

---
 tests/plugins/imagebase/features/link.js | 78 ++++++++++--------------
 1 file changed, 31 insertions(+), 47 deletions(-)

diff --git a/tests/plugins/imagebase/features/link.js b/tests/plugins/imagebase/features/link.js
index 9ede25f986b..d156c3d87e2 100644
--- a/tests/plugins/imagebase/features/link.js
+++ b/tests/plugins/imagebase/features/link.js
@@ -146,6 +146,26 @@
 		} );
 	}
 
+	function testLinkOptionInContextMenu( editor, bot, data, testCase, cb ) {
+		addTestWidget( editor );
+		bot.setData( data, function() {
+			var widget = editor.widgets.getByElement( editor.editable().findOne( 'figure' ) );
+
+			widget.focus();
+			editor.contextMenu.open( editor.editable() );
+			var itemsExist = 0;
+			for ( var i = 0; i < editor.contextMenu.items.length; ++i ) {
+				if ( testCase( editor, i ) ) {
+					itemsExist += 1;
+				}
+			}
+
+			editor.contextMenu.hide();
+
+			cb( itemsExist );
+		} );
+	}
+
 	var tests = {
 		'test adding image widget with link feature': function( editor ) {
 			var expectedParts = {
@@ -391,60 +411,24 @@
 			} );
 		},
 		'test link option in context menu': function( editor, bot ) {
-			addTestWidget( editor );
-			bot.setData( '<figure><a href="http://foo"><img src="%BASE_PATH%_assets/logo.png"></a></figure>', function() {
-				var widget = editor.widgets.getByElement( editor.editable().findOne( 'figure' ) );
-
-				widget.focus();
-				editor.contextMenu.open( editor.editable() );
-				var itemsExist = 0;
-				for ( var i = 0; i < editor.contextMenu.items.length; ++i ) {
-					if ( editor.contextMenu.items[ i ].command == 'link' ) {
-						itemsExist += 1;
-					}
-				}
-
-				editor.contextMenu.hide();
-
+			testLinkOptionInContextMenu( editor, bot, '<figure><a href="http://foo"><img src="%BASE_PATH%_assets/logo.png"></a></figure>', function( editor, index ) {
+				return editor.contextMenu.items[ index ].command == 'link';
+			}, function( itemsExist ) {
 				assert.areSame( 1, itemsExist, 'there is one link item in context menu' );
 			} );
 		},
 		'test unlink option in context menu': function( editor, bot ) {
-			addTestWidget( editor );
-			bot.setData( '<figure><a href="http://foo"><img src="%BASE_PATH%_assets/logo.png"></a></figure>', function() {
-				var widget = editor.widgets.getByElement( editor.editable().findOne( 'figure' ) );
-
-				widget.focus();
-				editor.contextMenu.open( editor.editable() );
-				var itemsExist = 0;
-				for ( var i = 0; i < editor.contextMenu.items.length; ++i ) {
-					if ( editor.contextMenu.items[ i ].command == 'unlink' ) {
-						itemsExist += 1;
-					}
-				}
-
-				editor.contextMenu.hide();
-
-				assert.areSame( 1, itemsExist, 'there is one link item in context menu' );
+			testLinkOptionInContextMenu( editor, bot, '<figure><a href="http://foo"><img src="%BASE_PATH%_assets/logo.png"></a></figure>', function( editor, index ) {
+				return editor.contextMenu.items[ index ].command == 'unlink';
+			}, function( itemsExist ) {
+				assert.areSame( 1, itemsExist, 'there is one unlink item in context menu' );
 			} );
 		},
 		'test no link option in context menu': function( editor, bot ) {
-			addTestWidget( editor );
-			bot.setData( '<figure><img src="%BASE_PATH%_assets/logo.png"></figure>', function() {
-				var widget = editor.widgets.getByElement( editor.editable().findOne( 'figure' ) );
-
-				widget.focus();
-				editor.contextMenu.open( editor.editable() );
-				var itemsExist = 0;
-				for ( var i = 0; i < editor.contextMenu.items.length; ++i ) {
-					if ( editor.contextMenu.items[ i ].command == 'unlink' || editor.contextMenu.items[ i ].command == 'link' ) {
-						itemsExist += 1;
-					}
-				}
-
-				editor.contextMenu.hide();
-
-				assert.areSame( 0, itemsExist, 'there should not be any link option in contextmenu' );
+			testLinkOptionInContextMenu( editor, bot, '<figure><img src="%BASE_PATH%_assets/logo.png"></figure>', function( editor, index ) {
+				return editor.contextMenu.items[ index ].command == 'unlink' || editor.contextMenu.items[ index ].command == 'link';
+			}, function( itemsExist ) {
+				assert.areSame( 0, itemsExist, 'there is one link item in context menu' );
 			} );
 		}
 	};

From 0f44e0a51c038378d892a53ccbe36d1782e400a8 Mon Sep 17 00:00:00 2001
From: Wojciech Wozniak <wwozniak92@gmail.com>
Date: Thu, 14 Dec 2017 14:47:24 +0100
Subject: [PATCH 7/9] Manual test clean up.

---
 tests/plugins/imagebase/features/manual/linkcontextmenu.md | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tests/plugins/imagebase/features/manual/linkcontextmenu.md b/tests/plugins/imagebase/features/manual/linkcontextmenu.md
index 8f7f51183fa..d157b19f5bb 100644
--- a/tests/plugins/imagebase/features/manual/linkcontextmenu.md
+++ b/tests/plugins/imagebase/features/manual/linkcontextmenu.md
@@ -1,9 +1,10 @@
-@bender-tags: 4.8.0, feature, 932
+@bender-tags: 4.8.1, feature, 932
 @bender-ui: collapsed
 @bender-ckeditor-plugins: sourcearea, wysiwygarea, floatingspace, toolbar, imagebase, link, htmlwriter, elementspath, contextmenu
 
 
-1. Right click to to add link.
+1. Focus widget.
+1. Add link using `link` option in top bar menu.
 1. Click "Source" button to check output.
 
 ## Expected

From 3b8c82229930d1f62d61610fb45bd45c44a71d0b Mon Sep 17 00:00:00 2001
From: Tomasz Jakut <vepomoc@gmail.com>
Date: Mon, 18 Dec 2017 13:57:15 +0100
Subject: [PATCH 8/9] Update manual test.

---
 .../features/manual/linkcontextmenu.md        | 31 +++++++++++++------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/tests/plugins/imagebase/features/manual/linkcontextmenu.md b/tests/plugins/imagebase/features/manual/linkcontextmenu.md
index d157b19f5bb..3838089382c 100644
--- a/tests/plugins/imagebase/features/manual/linkcontextmenu.md
+++ b/tests/plugins/imagebase/features/manual/linkcontextmenu.md
@@ -1,20 +1,31 @@
-@bender-tags: 4.8.1, feature, 932
+@bender-tags: 4.9.0, feature, 932
 @bender-ui: collapsed
 @bender-ckeditor-plugins: sourcearea, wysiwygarea, floatingspace, toolbar, imagebase, link, htmlwriter, elementspath, contextmenu
 
+## Scenario 1
 
-1. Focus widget.
-1. Add link using `link` option in top bar menu.
-1. Click "Source" button to check output.
+1. Right-click the first image to show the context menu.
 
-## Expected
+### Expected
 
-* There is created link.
+* There are no link connected options in the menu.
 
+## Scenario 2
 
-1. Try to remove link with context menu.
-1. Click "Source" button to check output.
+1. Add link to the first image in the editor.
+2. Right click on it to show the context menu.
+3. Click "Edit Link" and change URL of the link.
 
-## Expected
+### Excepted
 
-* The link should be reomved.
+* There are link connected options in the menu.
+* Editing link's URL actually changes link's URL.
+
+## Scenario 3
+
+1. Right-click the second image in the editor to show the context menu.
+2. Choose "Unlink".
+
+### Expected
+
+* Link is removed.

From 77c7652506088c165e3b4acc871134189d352901 Mon Sep 17 00:00:00 2001
From: Tomasz Jakut <vepomoc@gmail.com>
Date: Mon, 18 Dec 2017 14:31:40 +0100
Subject: [PATCH 9/9] Simplify condition.

---
 plugins/imagebase/plugin.js | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/plugins/imagebase/plugin.js b/plugins/imagebase/plugin.js
index 11529c5a979..f8d31440019 100644
--- a/plugins/imagebase/plugin.js
+++ b/plugins/imagebase/plugin.js
@@ -57,7 +57,7 @@
 			init: function() {
 				if ( this.editor.plugins.link && this.editor.contextMenu ) {
 					this.on( 'contextMenu', function( evt ) {
-						if ( this.parts.link || this.wrapper.getAscendant( 'a' ) ) {
+						if ( this.parts.link ) {
 							evt.data.link = evt.data.unlink = CKEDITOR.TRISTATE_OFF;
 						}
 					} );
@@ -69,6 +69,7 @@
 					// All of listeners registered later on make only sense when link plugin is loaded.
 					return;
 				}
+
 				if ( editor.contextMenu ) {
 					editor.addMenuGroup( 'imagebase', 10 );